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.

MySQL Update Executes In Wrong Order

See original GitHub issue

MySQL 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
absoluxcommented, Jul 24, 2017

My mistake. Actually the issue comes from compiler._prepInsert(data) which prepares the data and sorts here the columns names

1reaction
kurrestahlbergcommented, Jul 25, 2017

I created a PR: #2171

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is UPDATE guaranteed to execute the SET assignments in the ...
Mysql UPDATE documentation says no. Single-table UPDATE assignments are generally evaluated from left to right. For multiple-table updates, ...
Read more >
MySQL 8.0 Reference Manual :: 13.2.17 UPDATE Statement
UPDATE is a DML statement that modifies rows in a table. An UPDATE statement can start with a WITH clause to define common...
Read more >
MySQL bugs fixed by Aurora MySQL database engine updates
Fixed an issue where the EXECUTE and ALTER ROUTINE privileges weren't ... Fix wrong sorting order issue when multiple CASE functions are used...
Read more >
Introducing the MySQL UPDATE statement - Simple Talk
The ORDER BY clause specifies the order that rows should be updated. This can be useful in situations that might otherwise result in...
Read more >
MYSQL Error: Incorrect usage of UNION and ORDER BY?
1 Answer 1 · I try to run the above query and it still didn't randomize. · the first select 2 rows with...
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