sequelize: sequelize n:m associations with paranoid throws unique errors

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

About this issue

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

Most upvoted comments

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 😦

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