Skip to content

Same second query for paranoid tables returning soft deleted records #8202

Closed

Description

What you are doing?

sequelize.define('user', {
  //...column definitions
}, {
  paranoid: true
});

User.find({
  where: {
    id: req.params.id
  }
}).then((user) => {
  console.log(user);
  user.destroy().then(() => {
    User.find({
      where: {
        id: req.params.id
      }
    }).then((deletedUser) => {
      console.log(deletedUser);
      console.log(new Date(new Date().toUTCString()).toISOString());
    });
  });
});

//run a second later
User.find({
  where: {
    id: req.params.id
  }
}).then((user) => {
  console.log(user);
});

What do you expect to happen?

Second console.log should return null.

What is actually happening?

Second console.log is returning the object again with a deletedAt field entered that matches the third console.log's output. Remote MySQL server runs in UTC time. Occurs on local development stations in EST as well as servers running in UTC time. Similar to #7204.

Running another query a second later will return null.

Dialect: mysql
Database version: 5.7.11
Sequelize version: 4.7.5

Activity

  1. deployable commented on Sep 12, 2017

    @deployable

    I'm seeing the same thing using sqlite, though #7290 mentions moving from $gte to $gt was meant to be a fix for it.

    The find* queries use table.deleted_at > CURRENT_TIMESTAMP in the where clause.

    SELECT `id`, `group`, `created_at`, `updated_at`, `deleted_at` 
    FROM `groups` AS `group`
    WHERE (`group`.`deleted_at` > CURRENT_TIMESTAMP OR `group`.`deleted_at` IS NULL);`
    

    It might be related to CURRENT_TIMESTAMP only being second resolution, but datetime is millisecond

    sqlite> select strftime('%Y-%m-%d %H:%M:%f', CURRENT_TIMESTAMP);
    2017-09-12 09:10:12.000
    

    Running manual queries with constructed dates including milliseconds works as expected:

    sqlite> update groups set deleted_at = strftime('%Y-%m-%d %H:%M:%f', 'now'); select * from groups where deleted_at > strftime('%Y-%m-%d %H:%M:%f', 'now');
    sqlite> 
    

    Whereas using CURRENT_TIMESTAMP returns the "deleted" record like sequelize:

    sqlite> update groups set deleted_at = strftime('%Y-%m-%d %H:%M:%f', 'now'); select * from groups where deleted_at > CURRENT_TIMESTAMP;
    9e7453ca-4424-44f4-98fc-8f71e073c55a|test|2017-09-12 08:23:21.707 +00:00|2017-09-12 08:23:21.707 +00:00|2017-09-12 09:12:59.802
    
  2. deployable commented on Sep 12, 2017

    @deployable

    The same test suite I found the problem with on sqlite works as expected in mysql/maria/postgres.

  3. apricote commented on Sep 30, 2017

    @apricote

    A fix that is working for me is contained in PR #8204
    There is a test case missing though, so it can not be merged yet.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions