{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/5614","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/5614/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/5614/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/5614/events","html_url":"https://github.com/sequelize/sequelize/issues/5614","id":142326948,"number":5614,"title":"association scope (N:M) field always NULL","user":{"login":"sharkovich","id":7272898,"avatar_url":"https://avatars.githubusercontent.com/u/7272898?v=3","gravatar_id":"","url":"https://api.github.com/users/sharkovich","html_url":"https://github.com/sharkovich","followers_url":"https://api.github.com/users/sharkovich/followers","following_url":"https://api.github.com/users/sharkovich/following{/other_user}","gists_url":"https://api.github.com/users/sharkovich/gists{/gist_id}","starred_url":"https://api.github.com/users/sharkovich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sharkovich/subscriptions","organizations_url":"https://api.github.com/users/sharkovich/orgs","repos_url":"https://api.github.com/users/sharkovich/repos","events_url":"https://api.github.com/users/sharkovich/events{/privacy}","received_events_url":"https://api.github.com/users/sharkovich/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-03-21T11:59:55Z","updated_at":"2016-03-21T12:52:37Z","closed_at":"2016-03-21T12:48:48Z","body":"Hello. I have set up N:M scoped association very similar to the example from [http://docs.sequelizejs.com/en/latest/docs/associations/#nm](documentation), and the scope field in my database is always empty. I am using Sequelize v3.19.3 with MySQL database (v5.6.28). Those are my  model definitions:\r\n\r\n```\r\nvar Notification = sequelize.define(\"Notification\", {\r\n  ...\r\n}, {\r\n  classMethods: {\r\n    associate: function (models) {\r\n      Notification.belongsToMany(models.Post, {\r\n        through: {\r\n          model: models.NotificationTarget,\r\n          unique: false,\r\n        },\r\n        foreignKey: {\r\n          name: 'notificationToken',\r\n        },\r\n        constraints: false,\r\n        as: 'TargetPost',\r\n      });\r\n      \r\n      Notification.belongsToMany(models.Comment, {\r\n        through: {\r\n          model: models.NotificationTarget,\r\n          unique: false,\r\n        },\r\n        foreignKey: {\r\n          name: 'notificationToken',\r\n        },\r\n        constraints: false,\r\n        as: 'TargetComment',\r\n      });\r\n      ...\r\n    },\r\n  },\r\n});\r\n```\r\n\r\n```\r\nvar Post = sequelize.define('Post', {\r\n  token: {\r\n    type: DataTypes.STRING,\r\n    primaryKey: true,\r\n    unique: true,\r\n  },\r\n  ...\r\n}, {\r\n  classMethods: {\r\n    associate: function (models) {\r\n      ...\r\n      Post.belongsToMany(models.Notification, {\r\n        through: {\r\n          model: models.NotificationTarget,\r\n          unique: false,\r\n          scope: {\r\n            targetType: 'post',\r\n          },\r\n        },\r\n        foreignKey: 'targetToken',\r\n        constraints: false,\r\n      });\r\n    },\r\n  },\r\n});\r\n```\r\n\r\n```\r\nvar NotificationTarget = sequelize.define(\"NotificationTarget\", {\r\n  id: {\r\n    type: DataTypes.INTEGER,\r\n    autoIncrement: true,\r\n    unique: true,\r\n    primaryKey: true,\r\n  },\r\n  notificationToken: {\r\n    type: DataTypes.STRING,\r\n    unique: 'target_notification',\r\n  },\r\n  targetType: {\r\n    type: DataTypes.STRING,\r\n    unique: 'target_notification',\r\n  },\r\n  targetToken: {\r\n    type: DataTypes.STRING,\r\n    unique: 'target_notification',\r\n    references: null,\r\n  },\r\n});\r\n```\r\n\r\nI create new notification instance and add a post to it:\r\n```\r\ndb.Notification.create(data, {\r\n  include: {\r\n    all: true,\r\n  },\r\n}).then(function (notification) {\r\n  if (post) {\r\n    notification.addTargetPost(post);\r\n  }\r\n  notification.save();\r\n});\r\n```\r\n\r\nI get no error whatsover, new records are created in both `notification` and `notification_target` tables, but `notification_target.targetType` field is always set to `NULL`. \r\n\r\nWhen i tried to get the notification from db:\r\n\r\n```\r\ndb.Notification.findAll({\r\n  where: {\r\n    recipient: userToken,\r\n  },\r\n  include: [{\r\n      model: db.Comment,\r\n      as: 'TargetComment',\r\n    }, {\r\n      model: db.Post,\r\n      as: 'TargetPost',\r\n    }\r\n  ]\r\n});\r\n```\r\nfor quite some time i was getting `Notification` object with associated Posts or Comments just as expected. Now on my local development setup botth `TargetPost` and `TargetComment` arrays are always empty. I haven't made any changes, and in test enviorment i still get associated records just fine.\r\n\r\nInsert log:\r\n```\r\nExecuting (default): INSERT INTO `tb_notifications` (`token`,`isRead`,`type`,`sender`,`recipient`,`createdAt`,`updatedAt`) VALUES ('MMpB39MBoRE5',false,'has-liked','8BxmIea1WIOJ','2DegpO','2016-03-21 12:02:17','2016-03-21 12:02:17');\r\nExecuting (default): INSERT INTO `tb_notifications_target` (`id`,`notificationToken`,`targetToken`,`createdAt`,`updatedAt`) VALUES (NULL,'MMpB39MBoRE5','vretxC','2016-03-21 12:02:17','2016-03-21 12:02:17');\r\n```\r\n\r\nSelect log:\r\n```\r\nExecuting (default): SELECT `Notification`.`token`, `Notification`.`isRead`, `Notification`.`type`, `Notification`.`sender`, `Notification`.`recipient`, `Notification`.`createdAt`, `Notification`.`updatedAt`, `Sender`.`username` AS `Sender.username`, `Sender`.`name` AS `Sender.name`, `Sender`.`avatar` AS `Sender.avatar`, `Sender`.`email` AS `Sender.email`, `Sender`.`token` AS `Sender.token`, `TargetComment`.`token` AS `TargetComment.token`, `TargetComment`.`content` AS `TargetComment.content`, `TargetComment`.`deleted` AS `TargetComment.deleted`, `TargetComment`.`createdAt` AS `TargetComment.createdAt`, `TargetComment`.`updatedAt` AS `TargetComment.updatedAt`, `TargetComment`.`postToken` AS `TargetComment.postToken`, `TargetComment`.`userToken` AS `TargetComment.userToken`, `TargetComment`.`parentToken` AS `TargetComment.parentToken`, `TargetComment.NotificationTarget`.`id` AS `TargetComment.NotificationTarget.id`, `TargetComment.NotificationTarget`.`notificationToken` AS `TargetComment.NotificationTarget.notificationToken`, `TargetComment.NotificationTarget`.`targetType` AS `TargetComment.NotificationTarget.targetType`, `TargetComment.NotificationTarget`.`targetToken` AS `TargetComment.NotificationTarget.targetToken`, `TargetComment.NotificationTarget`.`createdAt` AS `TargetComment.NotificationTarget.createdAt`, `TargetComment.NotificationTarget`.`updatedAt` AS `TargetComment.NotificationTarget.updatedAt`, `TargetPost`.`id` AS `TargetPost.id`, `TargetPost`.`token` AS `TargetPost.token`, `TargetPost`.`published` AS `TargetPost.published`, `TargetPost`.`title` AS `TargetPost.title`, `TargetPost`.`canonicalTitle` AS `TargetPost.canonicalTitle`, `TargetPost`.`excerpt` AS `TargetPost.excerpt`, `TargetPost`.`createdAt` AS `TargetPost.createdAt`, `TargetPost`.`updatedAt` AS `TargetPost.updatedAt`, `TargetPost`.`deletedAt` AS `TargetPost.deletedAt`, `TargetPost`.`language` AS `TargetPost.language`, `TargetPost`.`status` AS `TargetPost.status`, `TargetPost`.`userToken` AS `TargetPost.userToken`, `TargetPost`.`coverPhoto` AS `TargetPost.coverPhoto`, `TargetPost`.`iconPhoto` AS `TargetPost.iconPhoto`, `TargetPost.NotificationTarget`.`id` AS `TargetPost.NotificationTarget.id`, `TargetPost.NotificationTarget`.`notificationToken` AS `TargetPost.NotificationTarget.notificationToken`, `TargetPost.NotificationTarget`.`targetType` AS `TargetPost.NotificationTarget.targetType`, `TargetPost.NotificationTarget`.`targetToken` AS `TargetPost.NotificationTarget.targetToken`, `TargetPost.NotificationTarget`.`createdAt` AS `TargetPost.NotificationTarget.createdAt`, `TargetPost.NotificationTarget`.`updatedAt` AS `TargetPost.NotificationTarget.updatedAt` FROM `tb_notifications` AS `Notification` LEFT OUTER JOIN `tb_users` AS `Sender` ON `Notification`.`sender` = `Sender`.`token` AND `Sender`.`deletedAt` IS NULL LEFT OUTER JOIN (`tb_notifications_target` AS `TargetComment.NotificationTarget` INNER JOIN `tb_comments` AS `TargetComment` ON `TargetComment`.`token` = `TargetComment.NotificationTarget`.`targetToken`) ON `Notification`.`token` = `TargetComment.NotificationTarget`.`notificationToken` LEFT OUTER JOIN (`tb_notifications_target` AS `TargetPost.NotificationTarget` INNER JOIN `tb_posts` AS `TargetPost` ON `TargetPost`.`token` = `TargetPost.NotificationTarget`.`targetToken`) ON `Notification`.`token` = `TargetPost.NotificationTarget`.`notificationToken` AND `TargetPost`.`deletedAt` IS NULL WHERE `Notification`.`recipient` = '2DegpO' AND `Notification`.`token` = 'MMpB39MBoRE5';\r\n```\r\n\r\nAny help and/or corrections would be very much appreciated."},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/199258022","html_url":"https://github.com/sequelize/sequelize/issues/5614#issuecomment-199258022","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/5614","id":199258022,"user":{"login":"mickhansen","id":699049,"avatar_url":"https://avatars.githubusercontent.com/u/699049?v=3","gravatar_id":"","url":"https://api.github.com/users/mickhansen","html_url":"https://github.com/mickhansen","followers_url":"https://api.github.com/users/mickhansen/followers","following_url":"https://api.github.com/users/mickhansen/following{/other_user}","gists_url":"https://api.github.com/users/mickhansen/gists{/gist_id}","starred_url":"https://api.github.com/users/mickhansen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mickhansen/subscriptions","organizations_url":"https://api.github.com/users/mickhansen/orgs","repos_url":"https://api.github.com/users/mickhansen/repos","events_url":"https://api.github.com/users/mickhansen/events{/privacy}","received_events_url":"https://api.github.com/users/mickhansen/received_events","type":"User","site_admin":false},"created_at":"2016-03-21T12:52:37Z","updated_at":"2016-03-21T12:52:37Z","body":"Correct, if the main model needs to be filtered by the include you need `required: true`"}}