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.

Model.destroy with limit and composite primary key - deleting more rows than expected

See original GitHub issue

Sequelize: 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:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mickhansencommented, Sep 10, 2015
GroupMember
    .destroy({
        where: { groupId: groupId, userId: memberId },
        limit: 1,
        force: true
    })

This will likely never work. But i assume you would expect calling destroy on an instance of GroupMember to work (which effectively does the same as the above code but should have more information)

0reactions
armellarciercommented, Sep 11, 2018

@mickhansen could you elaborate on your comment ?

This will likely never work.

This may lead to corruption and I can’t see the problem with the usage that the reporter @tiblu made of the API…

Read more comments on GitHub >

github_iconTop 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 >

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