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.

afterBulkCreate hook unexpectedly firing on relationship model

See original GitHub issue

I have a User model. Users can have many followers/followees via the Relationship model.

When I add a relationship via user1.addFollower(user2), I’d expect the individual afterCreate hook on Relationship to fire, but the afterBulkCreate hook fires instead.

Steps to reproduce:

'use strict';

const Sequelize = require('sequelize');

let sequelize = new Sequelize(
    // your sandbox details here
);

let User = sequelize.define('user', {
    id: {primaryKey: true, type: Sequelize.UUID, defaultValue: Sequelize.UUIDV4}
});

let Relationship = sequelize.define('relationship', {
    id: {primaryKey: true, type: Sequelize.UUID, defaultValue: Sequelize.UUIDV4}
}, {
    hooks: {
        afterCreate: () => console.log('Relationship.afterCreate'),
        afterBulkCreate: () => console.log('Relationship.afterBulkCreate')
    }
});

User.belongsToMany(User, {as: 'Followers', through: Relationship, foreignKey: 'followee_id'});
User.belongsToMany(User, {as: 'Followees', through: Relationship, foreignKey: 'follower_id'});

let user1, user2;
sequelize.sync({force: true}).
    then(() => User.create()).
    then(u => user1 = u).
    then(() => User.create()).
    then(u => user2 = u).
    then(() => user1.addFollower(user2));

I expected to see: Relationship.afterCreate printed to the console. I actually saw: Relationship.afterBulkCreate.

Full output:

Executing (default): INSERT INTO "users" ("id","createdAt","updatedAt") VALUES ('e2f1ad28-1324-4ce8-91d6-9ad7f6577fdb','2016-07-18 17:58:34.565 +00:00','2016-07-18 17:58:34.565 +00:00') RETURNING *;
Executing (default): INSERT INTO "users" ("id","createdAt","updatedAt") VALUES ('d66c5992-140f-4f14-a913-3ee0629255c7','2016-07-18 17:58:34.575 +00:00','2016-07-18 17:58:34.575 +00:00') RETURNING *;
Executing (default): SELECT "id", "createdAt", "updatedAt", "followee_id", "FollowerId" FROM "relationships" AS "relationship" WHERE "relationship"."followee_id" = 'e2f1ad28-1324-4ce8-91d6-9ad7f6577fdb' AND "relationship"."FollowerId" IN ('d66c5992-140f-4f14-a913-3ee0629255c7');
Executing (default): INSERT INTO "relationships" ("id","followee_id","FollowerId","createdAt","updatedAt") VALUES ('5ba1aa52-ebb7-419f-ac77-3de558626e21','e2f1ad28-1324-4ce8-91d6-9ad7f6577fdb','d66c5992-140f-4f14-a913-3ee0629255c7','2016-07-18 17:58:34.589 +00:00','2016-07-18 17:58:34.589 +00:00');
Relationship.afterBulkCreate

Dialect: postgres Database version: 9.5.3 Sequelize versions: noticed on 3.19.3, reproduced on latest 3.23.4

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sushantdhimancommented, Sep 23, 2016

@harto I am closing the issue for now, if there is more demand for changing the hooks for single instance cases we will consider 😃

0reactions
jduhlscommented, Sep 23, 2016

Just adding a tip that is related:

You will also get bulkBeforeCreate instead of beforeCreate and apparently (due to hook execution order?) if you want to trigger beforeCreate you can addFollower(follower, {individualHooks: true}). I was not able to trigger afterCreate I assume because it is BEFORE afterBulkCreate in order of operations.

src: http://sequelize.readthedocs.io/en/latest/docs/hooks/#order-of-operations

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks - Sequelize
The diagram below shows the firing order for the most common hooks. Note: this list is not exhaustive. (1) beforeBulkCreate(instances, options)
Read more >
sequelize.js hook afterBulkCreate iteration - Stack Overflow
I'm having problems getting an afterBulkCreate hook to work using promises. Its being fired, but I get strange errors.
Read more >
Manual | Sequelize - ESDoc Hosting Service
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, ...
Read more >
Decoupling Logic with Domain Events [Guide] - Khalil Stemmler
Hooking into succesful transactions with Sequelize. Using Sequelize, we can define a callback function for each hook that takes the model name ...
Read more >
download file - PDFCOFFEE.COM
Learn how to use Object-Oriented Programming and Domain modeling to tackle ... I didn't get too far without studying basic UML relationships again....
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