Search code examples
javascriptmysqlnode.jssequelize.jspostman

Query was empty Nodejs Sequlize


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?


Solution

  • 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();
    }