Skip to content

add* and set* methods from belongsToMany raise SequelizeEmptyResultError if rejectOnEmpty is set to true in the target model #9531

Closed

Description

@sylver

What are you doing?

A self reference with belongsToMany method as explain in the doc :

http://docs.sequelizejs.com/manual/tutorial/associations.html#belongs-to-many-associations

User.belongsToMany(User, {
  as: 'Blacklists',
  through: 'users_blacklists',
  foreignKey: 'user_id',
  otherKey: 'blocked_user_id',
})

Note: The problem is reproductible with a classic (non self reference) belongsToMany association

What do you expect to happen?

It should create an association in the users_blacklists table with User instances I pass to methods addBlacklist, addBlacklists or setBlacklists, like :

user.addBlacklists([userBlocked1, userBlocked2]).then(() => {
  // saved!
})
// Where `user`, `userBlocked1` and `userBlocked2` are valid existing `User` instances

What is actually happening?

I get a SequelizeEmptyResultError instead when using those methods.

The SQL output shows that Sequelize is trying to do a SELECT instead of an INSERT whether I use addBlacklist, addBlacklists or setBlacklists, which obviously explains the SequelizeEmptyResultError since Sequelize did not insert those entries in the table yet.

SELECT "created_at", "updated_at", "user_id", "blocked_user_id" FROM "users_blacklists" AS "users_blacklists" WHERE "users_blacklists"."user_id" = '3ec3a02a-9ad7-4830-9f4e-a74256caee9e' AND "users_blacklists"."blocked_user_id" IN ('6c604b1d-5c43-483e-963d-b4a264461731', 'cb51ab45-3eb6-44cb-a2a5-5fcd1f5b2989');

EDIT : Found the problem's cause

After some research and digging in sequelize's code, it appears you are doing a findAll first to check if the added instances already exist in the join table :

return association.through.model.findAll(_.defaults({where, raw: true}, options)).then(currentRows => {

If the option rejectOnEmpty is set to true in the target model definition, this findAll raise the SequelizeEmptyResultError error.
Now, this is really weird since this is the join table which is queried here, not the target table having this option defined, so it shouldn't raise, but even if it does, you should catch it.

I can disable the rejectOnEmpty for now to bypass this issue, but this is triggering side effects in my code I'm not happy with.
I'll try to make a quick and simple PR for this issue unless you can quickly fix this problem.

Thanks

Dialect: postgres
Dialect version: 9.6.9
Database version: 9.6.9
Sequelize version: 4.37.10
Tested with latest release: 4.37.10

Activity

  1. changed the title [-]Unexpected belongsToMany behavior with self references[/-] [+]add* and set* methods from belongsToMany raise SequelizeEmptyResultError if rejectOnEmpty is set to true in the target model[/+] on Jun 13, 2018
  2. added
    type: bug
    on Jun 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions