Skip to content

[docs] Does creating a new instance with the same primary key after a soft delete work? #9885

Closed

Description

@jedwards1211

I can't tell from the docs what will happen if I soft delete instance 42 on a paranoid mode model, and then later create a new instance with that same key. Does Sequelize overwrite the fields on the soft deleted row and set deletedAt to null in that case, or does it throw a duplicate key error?

Activity

  1. sushantdhiman commented on Sep 13, 2018

    @sushantdhiman
    Contributor

    Easy to test

    const Task = sequelize.define("task", {
      name: { type: Sequelize.STRING },
    }, {
      timestamps: true,
      paranoid: true
    });
    
    (async () => {
      await sequelize.sync({force: true});
    
      try {
        const t1 = await Task.create({ name: 'a' });
        await t1.destroy();
    
        const t2 = await Task.create({ id: t1.get('id'), name: 'a' });
        
        console.log(t1.toJSON());
        console.log(t2.toJSON());
      } catch (e) {
        console.error(e);
      }
    
      await sequelize.close();
    })();
    SequelizeUniqueConstraintError: Validation error
        at Query.formatError (/var/www/github/sequelize/lib/dialects/postgres/query.js:326:18)
        at query.catch.err (/var/www/github/sequelize/lib/dialects/postgres/query.js:86:18)
    From previous event:
        at Query.run (/var/www/github/sequelize/lib/dialects/postgres/query.js:79:23)
        at Promise.try.then.connection (/var/www/github/sequelize/lib/sequelize.js:547:20)
    From previous event:
        at Promise.resolve.retryParameters (/var/www/github/sequelize/lib/sequelize.js:544:8)
        at /var/www/github/sequelize/node_modules/retry-as-promised/index.js:58:21
        at new Promise (<anonymous>)
        at retryAsPromised (/var/www/github/sequelize/node_modules/retry-as-promised/index.js:48:10)
        at Sequelize.query (/var/www/github/sequelize/lib/sequelize.js:443:28)
        at QueryInterface.insert (/var/www/github/sequelize/lib/query-interface.js:837:27)
        at Promise.try.then.then.then (/var/www/github/sequelize/lib/model.js:3779:53)
        at runCallback (timers.js:810:20)
        at tryOnImmediate (timers.js:768:5)
        at processImmediate [as _immediateCallback] (timers.js:745:5)
    From previous event:
        at model.save (/var/www/github/sequelize/lib/model.js:3755:8)
        at Function.create (/var/www/github/sequelize/lib/model.js:2187:8)
        at /var/www/github/sequelize/sscce.js:20:27
        at <anonymous>
  2. jedwards1211 commented on Sep 13, 2018

    @jedwards1211
    ContributorAuthor

    @sushantdhiman Yes, but the main issue is that we should document it.

  3. jedwards1211 commented on Sep 13, 2018

    @jedwards1211
    ContributorAuthor

    @sushantdhiman also, since the behavior wasn't documented, I couldn't assume that the actual behavior is the intended behavior and not a bug, which is why it's important to ask these kinds of questions

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions