question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update statements with joins don't seem to work

See original GitHub issue

Hello! Just switched from Prisma and loving the library so far. Just ran into an issue where I’m hoping there’s a workaround or I’m doing something wrong.

Basically, I have an existing MySQL statement that reads:

update OverallMV O join OverallMV O2 on O.Rank = O2.Rank and O.UserID <> O2.UserID set O.Tied = true

So I recreated it in Kysely as:

await trx.updateTable("OverallMV as O").innerJoin(
      "OverallMV as O2",
      (join) =>
        join
          .onRef("O.Rank", "=", "O2.Rank")
          .onRef("O.UserID", "<>", "O2.UserID"),
    ).set({ Tied: 1 }).execute();

However, this fails with a ER_PARSE_ERROR since it puts the set before the join:

update `OverallMV` as `O` set `Tied` = 1 inner join `OverallMV` as `O2` on `O`.`Rank` = `O2`.`Rank` and `O`.`UserID` <> `O2`.`UserID`

Any ideas here?

Side note, I’m wondering if this use case was never tested as I would also expect to choose which table to update in the set. So more like .set({ "O.Tied": 1 }), which does not currently work but .set({ Tied: 1 }) does.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
koskimascommented, Oct 14, 2022

That wouldn’t work with the types. Correct me if I’m wrong, but on MySQL you can update all joined tables? So we would need to append to UT of UpdateQueryBuilder<DB, UT, TB, O> in “before from joins” and to TB in “after from joins”. We’d need to add a new type argument just to indicate if from has been called.

1reaction
koskimascommented, Oct 12, 2022

I think what @igalklebanov suggested regarding WITH is this:

trx
  .with('someGoodName', (qb) => qb
    .selectFrom("OverallMV as O")
    .innerJoin("OverallMV as O2", (join) => join
      .onRef("O.Rank", "=", "O2.Rank")
      .onRef("O.UserID", "<>", "O2.UserID")
    )
    .select('ID')
  )
  .updateTable("OverallMV")
  .whereExists((eb) => eb.selectFrom("someGoodName").select("ID"))
  .set({ Tied: 1 })
Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL - Inner join not working with update statement
UPDATE `Student` INNER JOIN `Year` on Student.GradYear = Year.GradYear and Year.UserID = 1 SET Student.Half = 0;. ^this should work.
Read more >
5 Useful Tips for Using SQL Server UPDATE Query with JOIN
Using Update Command with Join Safely (5 Tips) · View the Records First with the SELECT Statement · Do a Trial Run Using...
Read more >
SQL Update Join not updating all records - MSDN
Using SQL Server 2008 R2. I have a select join query that will return 2202 records: SELECT * FROM bAPVM V INNER JOIN...
Read more >
SQL UPDATE from SELECT, JOIN or MERGE
In this article learn how to update data in a SQL Server table from another table using a JOIN, the MERGE statement or...
Read more >
Using A SQL JOIN In A SQL UPDATE Statement (Thanks John ...
I have known for a long time that you could update a SQL View in Microsoft SQL Server (back when I used to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found