-1

I'm trying to update data in a nodejs application. I tested it with postman.

My developing steps was was:

  1. Getting data with id: 10 from DB (MySQL) to update >> Unhandled rejection SequelizeDatabaseError: Query was empty

  2. I recognised, I use wrong id, so I changed to a correct one:50 >> Some other error

  3. Okay, I fix this some other error related to my inaccurate work, and use good id: 50 >> Unhandled rejection SequelizeDatabaseError: Unhandled rejection SequelizeDatabaseError: Query was empty
  4. Strange behavior, because this id was good earlier... (in the 3. step) Okay, I just change the id to an other correct one: 51 >> Some other error
  5. Okay, I fix the other error, try the update again with the new-new id: 51 >> Unhandled rejection SequelizeDatabaseError: Query was empty

It seems to me, I can get the valid data just one time, after I can not reach it... Full stacktrace:

Unhandled rejection SequelizeDatabaseError: Query was empty
    at Query.formatError (C:\Users\bla_bla_bla\Documents\work\_nodejs\parentfolder\application\application\node_modules\sequelize\lib\dialects\mysql\query.js:223:16)
    at Query.connection.query [as onResult] (C:\Users\bla_bla_bla\Documents\work\_nodejs\parentfolder\application\application\node_modules\sequelize\lib\dialects\mysql\query.js:55:23)
    at Query.Command.execute (C:\Users\bla_bla_bla\Documents\work\_nodejs\parentfolder\application\application\node_modules\mysql2\lib\commands\command.js:30:12)
    at Connection.handlePacket (C:\Users\bla_bla_bla\Documents\work\_nodejs\parentfolder\application\application\node_modules\mysql2\lib\connection.js:515:28)
    at PacketParser.onPacket (C:\Users\bla_bla_bla\Documents\work\_nodejs\parentfolder\application\application\node_modules\mysql2\lib\connection.js:94:16)
    at PacketParser.executeStart (C:\Users\bla_bla_bla\Documents\work\_nodejs\parentfolder\application\application\node_modules\mysql2\lib\packet_parser.js:77:14)
    at Socket.<anonymous> (C:\Users\bla_bla_bla\Documents\work\_nodejs\parentfolder\application\application\node_modules\mysql2\lib\connection.js:102:29)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:547:20

Ideas?

Note

My problem is not related to model-DB syncronisation. These are syncronised, because I have a case - the first calling - when I can update the object! But if I test that id twice, third etc... I getting this error. So, if these are nor synconised, then I could not update the obejct in the first time neither

UPDATE

After debbing a day, I just recognised, the second and the third time the sequlize making empty SQL query. Below the code snippets:

The good SQL update:

Executing (default): UPDATE `cardModules` SET `field1`='xxxxxxxx',`field2`='xxxxxxxxxx',`field3`='MEDIUM',`field4`=false,`field5`=false WHERE `id` = 43

The bad SQL update:

Executing (default):

The question is: why does Sequlize this?

5
  • may be this will help github.com/sequelize/sequelize/issues/5223 Jan 2, 2018 at 13:22
  • @BinitGhetiya I checked this article before. Ther is an undefined field error in this cade, but in my case All of my field are filled. I get get the data but just one the, second I get this &*@{Đ@& error: Query was empty. Jan 2, 2018 at 13:30
  • please double confirm your all table columns are defined in class (model) Jan 2, 2018 at 13:31
  • @BinitGhetiya My problem is not related to model-DB syncronisation. These are syncronised, because I have a case - the first calling - when I can update the object! But if I test that id twice, third etc... I getting this error. So, if these are nor synconised, then I could not update the obejct in the first time neither. Jan 2, 2018 at 13:42
  • understand your query can you please add this in description also . Jan 2, 2018 at 13:46

2 Answers 2

7

To shed more light on how/why this can happen: Generally, Sequelize will track changes to your model and then when you call model.save(), it will only attempt to update SQL columns that have changed.

let person = async Employee.findOne();
person.name = "Jerry Jones";  
person.save();  //All good here, and only the name column will be updated

If no properties have changed, it won't even try to make the SQL call

let person = async Employee.findOne();
person.save();  //No errors, but no SQL call will be made

However, any properties set to an 'undefined' value (which is different than null!) will still cause the model to be marked as dirty/changed, but will be ignored when the SQL call is constructed. Thus, if you only change one property and set it to undefined, Sequelize will still attempt to make the call, but the query will be empty.

let userUpdates = {age: 99, title: "Developer"};
let person = async Employee.findOne();
person.name = userUpdates.name;  //name is undefined, but property will still be considered changed.
person.save();                   //Query will be empty!!
5

Number of hours I just recognised the problem. If the sequlize don't find any difference in the DB values and the updateable values, then will generate an empty SQL string and want to execute this. So becaues of this the same sequlize throws this Query was empty message.

So if there is no changes but you want to update, you will get this error.

I handle this problem to check the error message like:

            if(err.message == 'Query was empty'){
                console.log('There is no changes in the update, lets continue the progress...');
                next();
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.