question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Paranoid with unique constraint

See original GitHub issue

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don’t fill in the requested information
  • I have read the contribution guidelines

Bug Description

When we try to insert a record into a table with paranoid: true that has a soft deleted record with a similar unique field, a validation error is being thrown.

Reproducible Example

  class User extends Model {}

  User.init({
    username: {
      type: DataTypes.TEXT,
      allowNull: false,
      unique: true
    },
  }, {
    sequelize,
    modelName: 'User',
    paranoid: true,
  });
  
  const user = await User.create({
    username: 'foo',
  });
  
  await user.destroy();
  
  user.isSoftDeleted(); // true
  
  const anotherUser = await User.create({
    username: 'foo',
  }); // Fails

What do you expect to happen?

The creation of anotherUser should be successful, maybe by using upsert internally to change the createdAt timestamp to current date and deletedAt to null when a matching soft deleted record is found.

What is actually happening?

Validation error is being thrown.

Environment

  • Sequelize version: 6.x.x
  • Node.js version: 16.6.2
  • If TypeScript related: TypeScript version: No significance.
  • Database & Version: Postgres 14
  • Connector library & Version: pg

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don’t have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don’t have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as “+1” will be removed.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zigniscommented, Sep 16, 2022

It is database behaviour and should not be avoided by doing reset on the deleted_at field, why?

Because you didn’t actually delete the unique field, it is normal when you get the validation thrown error when you inserts the same username.

To solve this you must delete the record completely with it’s associations instead of soft deleting it.

upsert does not update the primary key, and it is dangerous for relationships, you should not using soft delete (to replace it with the new different user with the same primary key) for one-to-many associations, if you saving some transactions records, save it as historical data.

Reset the deleted_at to null back is not best practice.

Makes sense 👍

0reactions
fzn0xcommented, Sep 16, 2022

It is database behaviour and should not be avoided by doing reset on the deleted_at field, why?

Because you didn’t actually delete the unique field, it is normal when you get the validation thrown error when you inserts the same username.

To solve this you must delete the record completely with it’s associations instead of soft deleting it.

upsert does not fit to update the primary key, and it is dangerous for relationships, you should not using soft delete (to replace it with the new different user with the same primary key) for one-to-many associations, if you saving some transactions records, save it as historical data.

Reset the deleted_at to null back is not best practice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize Unique constraint + paranoid - Stack Overflow
I have a table that has a unique constraint on a field + paranoid. In the example below the brand name is the...
Read more >
sequelize n:m associations with paranoid throws unique errors
I did not want the system to violates unique constraint "FooBars_pkey" but it does. Another bug report mentions that paranoid should be ...
Read more >
Paranoid - Sequelize
Sequelize supports the concept of paranoid tables. A paranoid table is one that, when told to delete a record, it will not truly...
Read more >
[Solved]-Sequelize Unique constraint + paranoid-sequelize.js
I think the cleanest solution, the one sequelize's paranoid feature was created to handle, is to restore the original record. After you restore,...
Read more >
Paranoid Tables in Sequelize ORM, Implementing Soft Delete
Sequelize supports a feature called paranoid tables. The specialty about these tables is that when they are told to delete a record they ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found