-
-
Notifications
You must be signed in to change notification settings - Fork 544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query Builder onConflict with merge don't generate the correct sql #1774
Comments
edit: actually not, the problem is in the empty |
Merge docs in knew says: |
@B4nan so I need to call Thanks! Edit: If I call with params it generates a wrong sql em.createQueryBuilder(this).insert(data).onConflict("name").merge(["id_province"]).getFormattedQuery(); insert into `poblations` (`id_province`, `name`) values (44, 'Province 1'), (44, 'Province 2') on duplicate key update `0` = 'id_province' Knex says it generates something like this: insert into `poblations` (`id_province`, `name`) values (44, 'Province 1'), (44, 'Province 2') on duplicate key update `id_province` = values(`id_province`) |
Didn't know the parameter can be also array, that's actually different problem. It is not even allowed on the TS level, so the example you gave should result in TS error I guess it is working because you are passing the |
Unfortunately the https://github.com/knex/knex/blob/master/CHANGELOG.md#0950---03-march-2021 knex 0.95 is breaking as it requires node and TS version bumps, it is already working in v5, so if you feel brave enough, you could give the dev version a try. keep in mind there will be more breaking changes, but so far it was mostly just about the version bumps and internals, not public api. The fix is available as |
In fact I don't wan't to give params to merge, so it's nice! I will try tomorrow. Thanks for your work!! |
@B4nan it works like a charm! Would be nice to make the field param in em.createQueryBuilder(this).insert(data).onConflict("name").merge().getFormattedQuery();
// Following are empty or non existing fields
em.createQueryBuilder(this).insert(data).onConflict("test").merge().getFormattedQuery();
em.createQueryBuilder(this).insert(data).onConflict("").merge().getFormattedQuery(); The three lines generates the same, the field param doesn't seem to affect (at least in MariaDB): insert into `poblations` (`id_province`, `name`) values (44, 'Province 1'), (44, 'province 2') on duplicate key update `id_province` = values(`id_province`), `name` = values(`name`) |
I see, I thought the parameter is mandatory in knex, but apparently it is not. Will handle that as part of #1803 so subscribe there for updates. |
Describe the bug
Using Query Builder method onConflict with merge don't make the ON DUPLICATE in the resulted SQL.
Also the column/s required in onConflict don't make difference in the ON DUPLICATE, so would be nice to make optional at least.
Code
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Output:
Additional context
https://knexjs.org/#Builder-onConflict
https://mariadb.com/kb/en/insert-on-duplicate-key-update/
Versions
The text was updated successfully, but these errors were encountered: