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: open
- Created 2 years ago
- Comments: 15 (10 by maintainers)
You’re right.
Types are too complicated and its surprising to consumers (
join
andset
switch places in compiled sql because of afrom
existing 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.
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
ofUpdateQueryBuilder<DB, UT, TB, O>
in “beforefrom
joins” and toTB
in “afterfrom
joins”. We’d need to add a new type argument just to indicate iffrom
has been called.I think what @igalklebanov suggested regarding
WITH
is 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
set
method is used on all dialects and if you’d get autocompletion fortable.column
fields, people would definitely try to use them on all dialects.