kysely: Update statements with joins don't seem to work
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.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 17 (11 by maintainers)
Commits related to this issue
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 (#1079) — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 (#1079) — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 (#1079) — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 (#1079) — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 (#1079) — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 (#1079) — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 (#1079) — committed to kysely-org/kysely by koskimas 2 years ago
- Support update table1, table2, ... query support. Closes #192 (#1079) — committed to igalklebanov/kysely by koskimas 2 years ago
You’re right.
Types are too complicated and its surprising to consumers (
joinandsetswitch places in compiled sql because of afromexisting or not).I think expanding
.updateTable(...)as follows:update table t0, t1, t2, ...syntax.update table t0 inner join t1 ... left join t2 ...syntax.Would be aligned with consumer’s expectations of affecting the query’s main clause, and will make “downstream” types consistent and simpler.
I’m finally adding support for
update t1, t2 set t1.foo = ?, t2.bar = ? where t1.x = t2.yqueries here.+1 for @igalklebanov 's suggestion to support an array of table references:
Very surprising that this doesn’t work but I get the complications, Typescript can be a curse sometimes 😅
Any updates on @igalklebanov’s suggestion? I’m specifically interested in
and think this looks like a good option 👍🏼
Would be amazing if this could be considered if it works like that.
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
UTofUpdateQueryBuilder<DB, UT, TB, O>in “beforefromjoins” and toTBin “afterfromjoins”. We’d need to add a new type argument just to indicate iffromhas been called.I’ll definitely consider supporting joins like you suggested (and I have considered it), but it can be very difficult because of the other way of using joins in update statements (in a
fromclause).I think what @igalklebanov suggested regarding
WITHis this:It’s ok to add custom methods that don’t exist on other dialects. People don’t call them accidentally (well at least not as easily). But the
setmethod is used on all dialects and if you’d get autocompletion fortable.columnfields, people would definitely try to use them on all dialects.This would only work on MySQL AFAIK, so I haven’t supported that syntax.