MySQL Update Executes In Wrong Order
See original GitHub issueMySQL differs from the SQL spec in update by using whatever is the current value in a field at the time of evaluating an expression. More here: https://dev.mysql.com/doc/refman/5.7/en/ansi-diff-update.html
Knex seems to always order all set statements in alphabetical order.
This causes a problem in a statement like the following for example:
UPDATE table SET col1=col1+1, col2=col1*3;
In this case let’s assume I want col2 to be three times col1’s value AFTER the update. The statement above would work correctly for MySQL as col1’s new value will be used for calculating col2. Unfortunately at least MariaDB seems to follow the SQL spec and actually use the value of col1 from before the update and therefore store the wrong result.
I could fix this for both databases by doing UPDATE table set col2=(col1 + 1)*3,col1=col1+1; as then col1 would have the old value with both databases when calculating the value of col2. The problem is that Knex seems to reorder all fields to be updated to alphabetical order and therefore take away the possibility to make sure the correct result is stored with both databases.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:11 (9 by maintainers)

Top Related StackOverflow Question
My mistake. Actually the issue comes from
compiler._prepInsert(data)which prepares the data and sorts here the columns namesI created a PR: #2171