Skip to content

Invalid logging of single INSERT and UPDATE SQL queries #10067

Closed

Description

@ezze

What are you doing?

Here is a repo reproducing the issue.

In Sequelize 5.0.0-beta.13:

const connection = new Sequelize(database, username, password, {
    dialect,
    host,
    define: {
        freezeTableName: true,
        underscored: false,
        createdAt: 'createdAt',
        updatedAt: 'updatedAt'
    },
    logging: message => {
        console.log(message);
    }
});

const Task = connection.define('task', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    createdAt: {
        field: 'created_at',
        type: Sequelize.DATE,
        allowNull: false
    },
    updatedAt: {
        field: 'updated_at',
        type: Sequelize.DATE,
        allowNull: false
    },
    name: {
        type: Sequelize.TEXT,
        allowNull: false
    },
    status: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0
    }
});

(async() => {
    ...
    await Task.create({ name: 'First task', status: 1 }, { transaction });
    await Task.update({ status: 2 }, { where: { status: 1 }, transaction });
})();

What do you expect to happen?

Output valid SQL queries with inserted binded parameters' values in console:

INSERT INTO "task" ("id","created_at","updated_at","name","status") VALUES (DEFAULT,'2018-10-23 13:58:05.380 +00:00','2018-10-23 13:58:05.380 +00:00','First task',1) RETURNING *
UPDATE "task" SET "status"=2,"updated_at"='2018-10-23 13:58:05.398 +00:00' WHERE "status" = 1

What is actually happening?

I get SQL queries with tokens starting with $ instead of parameters' values:

INSERT INTO "task" ("id","created_at","updated_at","name","status") VALUES (DEFAULT,$1,$2,$3,$4) RETURNING *;
UPDATE "task" SET "status"=$1,"updated_at"=$2 WHERE "status" = $3

Note that bulkCreate method works fine.

Dialect: postgres
Dialect version: 7.5.0
Database version: 9.5.14
Sequelize version: 5.0.0-beta.13
Tested with latest release: Yes (latest beta version 5.0.0-beta.13)

Activity

  1. changed the title [-]Invalid logging of single `INSERT` and `UPDATE` SQL queries[/-] [+]Invalid logging of single INSERT and UPDATE SQL queries[/+] on Oct 23, 2018
  2. sushantdhiman commented on Oct 27, 2018

    @sushantdhiman
    Contributor

    Sequelize v5 is using bound parameters for INSERT and UPDATE statements. I guess we can append data in query logs

  3. added
    type: feature
    on Oct 27, 2018
  4. spearmootz commented on Nov 27, 2018

    @spearmootz
    Contributor

    this is still happening in beta.14

  5. lerit commented on Jan 7, 2019

    @lerit

    same question here

  6. sheldhur commented on Mar 23, 2019

    @sheldhur

    As a temporary solution, you can get bindings from the logger args.

    (sql, ...args) => {
      let instance = args.pop();
      let benchmark = args.pop();
    
      console.log(sql, benchmark, instance.bind);
    }

    PS. @sushantdhiman It would be nice to pass a logger args as one object {sql, benchmark, bind, instance}

  7. eggerand13 commented on Jun 25, 2019

    @eggerand13

    Is there already some kind of solution for this?
    Today we were forced to update to v5 due to the critical SQL injection vulnerability in Sequelize (https://snyk.io/vuln/SNYK-JS-SEQUELIZE-450221) but now we are lacking the ability to see the insert logs.

  8. superlbr commented on Jul 20, 2019

    @superlbr

    it's better to have a timetable for closing issues, not just let it hang, let pr hang

  9. papb commented on Jul 20, 2019

    @papb
    Member

    Hello @superlbr, I understand that you are anxious for fixes, we all are! Unfortunately, open source development has this kind of problems...

    If you really need the fix from that PR the way it currently is, read this comment I wrote on another issue and do something similar.

  10. superlbr commented on Jul 23, 2019

    @superlbr

    @papb for me, just modify code in node_modules, 1 min. What I suggest is merge pr first and implement later or by someone else.

  11. terrynguyen255 commented on Aug 20, 2019

    @terrynguyen255

    Sorry, any updates?

  12. added
    status: wip
    type: other
    and removed
    type: feature
    on Aug 20, 2019
  13. sushantdhiman commented on Sep 19, 2019

    @sushantdhiman
    Contributor

    Fixed by #11267

  14. hiteshgarg14 commented on Feb 6, 2020

    @hiteshgarg14

    Set logQueryParameters: true while creating a new Sequelize class instance.

    For eg,

    const sequelize = new Sequelize('database', 'username', 'password', {
      host: 'localhost',
      dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
      logQueryParameters: true,
    });
    
  15. ezze commented on Feb 26, 2020

    @ezze
    Author

    @sushantdhiman @papb I believe that it should be reopened. Still the issue in 5.21.5.

    I updated the repo to reproduce.

  16. ezze commented on Feb 26, 2020

    @ezze
    Author

    Ok, setting logQueryParameters to true doesn't work for me. Result is the same.

    const connection = new Sequelize(database, username, password, {
        dialect,
        host,
        define: {
            freezeTableName: true,
            underscored: false,
            createdAt: 'createdAt',
            updatedAt: 'updatedAt'
        },
        logQueryParameters: true,
        logging: message => {
            console.log(message);
        }
    });

    Anyway, if it would worked, there are still questions for me.

    1. Why do we need logQueryParameters at all and why do we need to log placeholders by default? v4 was working without it.

    2. Current default behavior is useless. Even if we consider that logging placeholders is what we need it doesn't work as expected. Placeholders are logged for single inserts and updates while values are logged for bulk inserts and selects.

  17. terrynguyen255 commented on Feb 27, 2020

    @terrynguyen255

    Works with "sequelize": "^5.21.2" @ezze

    const sequelize = new Sequelize(
      process.env.DB_DATABASE_NAME,
      process.env.DB_USER,
      process.env.DB_PASSWORD,
      {
        host: process.env.DB_HOST,
        port: process.env.DB_PORT,
        dialect: 'mysql',
        logQueryParameters: true,
        logging: statement => { console.log(statement); }
      }
    );

    However, I agree with you, for debugging, I have to copy params into placeholders before I can execute them at DB.

  18. ezze commented on Feb 27, 2020

    @ezze
    Author

    @Nogias9x, does it depend on dialect? I use postgres and logQueryParameters doesn' work for me with the repo I mentioned above.

  19. terrynguyen255 commented on Feb 28, 2020

    @terrynguyen255

    Sorry, I'm not sure @ezze

  20. phoenixweiss commented on May 7, 2021

    @phoenixweiss

    logQueryParameters: true still not working for a single update or insert in "sequelize": "^6.6.2".

  21. YavorK commented on Jun 10, 2021

    @YavorK

    As a temporary solution, you can get bindings from the logger args.

    (sql, ...args) => {
      let instance = args.pop();
      let benchmark = args.pop();
    
      console.log(sql, benchmark, instance.bind);
    }

    PS. @sushantdhiman It would be nice to pass a logger args as one object {sql, benchmark, bind, instance}

    I refined the function a bit because it was outputting too much info sometimes so here it is only for query + params

    function logToConsole(sql, ...args) {
        let instance = args.pop();
        console.log('---')
        // query has bound params (insert, delete, update)
        if(instance && instance.bind ){
            // log query + query params
            console.log(sql, instance.bind)
        } else {
            // log a query that has no bound params (select, raw)
            console.log(sql)
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: wiptype: other

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions