Paranoid with unique constraint
See original GitHub issueIssue 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:
- Created a year ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Makes sense 👍
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.