Model.destroy with limit and composite primary key - deleting more rows than expected
See original GitHub issueSequelize: 3.7.0 (worked on 2.x) DB: Postgresql 9.3
Syntax
GroupMember
.destroy({
where: { groupId: groupId, userId: memberId },
limit: 1,
force: true
})
.then(...);
Where GroupMember is a model NOT an instance.
Generated query
DELETE FROM
"GroupMembers"
WHERE "groupId" IN
(
SELECT
"groupId"
FROM
"GroupMembers"
WHERE "groupId" = 'a1522be6-cfe5-443d-83e0-01c95d477a81'
AND "userId" = '4e5622c0-705d-4a64-9f83-8c45eca0fe49' LIMIT 1
)
This query ignores the “userId” where codition and deletes all with “groupId” instead of “groupId” AND “userId”.
Expected query
DELETE FROM
"GroupMembers"
WHERE ctid IN (
SELECT
ctid
FROM "GroupMembers"
WHERE "groupId" = 'a1522be6-cfe5-443d-83e0-01c95d477a81'
AND "userId" = '4e5622c0-705d-4a64-9f83-8c45eca0fe49'
LIMIT 1
)
Postgres does not support LIMIT for DELETE statements, thus hidden ctid column and subselect are to be used to apply LIMIT.
More info
- Model definition:
var GroupMember = sequelize.define('GroupMember', {
groupId: {
type: DataTypes.UUID,
allowNull: false,
comment: 'Group id',
references: 'Groups',
referencesKey: 'id'
},
userId: {
type: DataTypes.UUID,
allowNull: false,
comment: 'User id',
references: 'Users',
referencesKey: 'id'
}
}
);
- GroupMembers has a composite primary key -
"GroupMembers_pkey" PRIMARY KEY, btree ("groupId", "userId")
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to delete multiple rows with 2 columns as composite ...
The answer in Oracle is: delete from cpi where (countryid, year) in (('AD', 2010), ('AF', 2009), ('AG', 1992)).
Read more >Defining Constraints and Indexes
A multi-column foreign key is known as a composite foreign key, and almost always references a table that has a composite primary key....
Read more >Working with items and attributes - Amazon DynamoDB
BatchWriteItem — Create or delete up to 25 items in one or more tables. ... For example, if a table has a composite...
Read more >Chapter 3. Data, Tables, and Database Design - O'Reilly
When you select the Cascade Delete option, if you delete a record whose primary key appears in another table as a foreign key,...
Read more >Frequently Asked Questions - SQLite
How do I add, delete or rename columns from an existing table in ... and '0.0' as the primary key on two different...
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 Free
Top 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

This will likely never work. But i assume you would expect calling
destroyon an instance of GroupMember to work (which effectively does the same as the above code but should have more information)@mickhansen could you elaborate on your comment ?
This may lead to corruption and I can’t see the problem with the usage that the reporter @tiblu made of the API…