afterBulkCreate hook unexpectedly firing on relationship model
See original GitHub issueI 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:
- Created 7 years ago
- Comments:8 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@harto I am closing the issue for now, if there is more demand for changing the hooks for single instance cases we will consider 😃
Just adding a tip that is related:
You will also get
bulkBeforeCreate
instead ofbeforeCreate
and apparently (due to hook execution order?) if you want to triggerbeforeCreate
you canaddFollower(follower, {individualHooks: true})
. I was not able to triggerafterCreate
I assume because it is BEFOREafterBulkCreate
in order of operations.src: http://sequelize.readthedocs.io/en/latest/docs/hooks/#order-of-operations