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.

sequelize n:m associations with paranoid throws unique errors

See original GitHub issue

Minimal repro

var Sequelize = require("sequelize"),
    sequelize = new Sequelize(process.env.DATABASE_URL, {
      define: {
        paranoid: true,
      },
      dialect: 'postgres'
    }),
    Foo = sequelize.define("Foo"),
    Bar = sequelize.define("Bar"),
    FooBar = sequelize.define("FooBar");

Foo.belongsToMany(Bar, { through: "FooBar" });
Bar.belongsToMany(Foo, { through: "FooBar" });

let myfoo, mybar;

sequelize.sync()
.then(() => Foo.create({}))
.then((foo) => {myfoo=foo; return Bar.create({});})
.then((bar) => {mybar=bar; return myfoo.setBars([mybar]);})
.then(() => myfoo.setBars([]))
.then(() => myfoo.setBars([mybar]))
.then(() => console.log("finish"))

What do you expect to happen?

I did not want the system to violates unique constraint “FooBars_pkey” but it does. Another bug report mentions that paranoid should be disabled for such associations but clearly it’s not. (https://github.com/sequelize/sequelize/issues/809)

Dialect: postgres Database version: “PostgreSQL 9.5.1 on x86_64-apple-darwin15.3.0, compiled by Apple LLVM version 7.0.2 (clang-700.1.81), 64-bit” Sequelize version: 3.24.3

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
akleibercommented, Apr 18, 2018

this issue is still a problem - if paranoid = true sequelize should set deletedAt to null on n:m relations that are already set

my workaround is to disable paranoid for relations 😦

2reactions
albertogasparincommented, Aug 31, 2018

I can confirm @oscarr-reyes findings. Assuming we have users, userTags and tags, and paranoid: true for userTags:

await user.addTag(tag); // fine first time
await user.removeTag(tag); // fine
await user.addTag(tag); // throws SequelizeUniqueConstraintError: Validation error
Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
How to return non-unique join table records in sequelize?
The problem resides in that Sequelize, as of v6.3.5, still doesn't directly support duplicate entries from through tables when using BelongToMany associations.
Read more >
Sequelize Unique constraint + paranoid - Stack Overflow
What exactly fails when you tried brand name + deleted_at unique constraint? · I rethought about it and changing\redefining the deleteAt field ...
Read more >
sequelize/sequelize - Gitter
However when hooks are executed for associations the option is not there ... I found that setting Unique: false and using sequelize db:migrate...
Read more >
Model definition - Manual | Sequelize
NOW }, // setting allowNull to false will add NOT NULL to the column, which means an error will be // thrown from...
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