{"action":"opened","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/8998","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/8998/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/8998/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/8998/events","html_url":"https://github.com/sequelize/sequelize/issues/8998","id":294490389,"number":8998,"title":"belongsToMany associations break with global default scope","user":{"login":"nwalters512","id":1476544,"avatar_url":"https://avatars2.githubusercontent.com/u/1476544?v=4","gravatar_id":"","url":"https://api.github.com/users/nwalters512","html_url":"https://github.com/nwalters512","followers_url":"https://api.github.com/users/nwalters512/followers","following_url":"https://api.github.com/users/nwalters512/following{/other_user}","gists_url":"https://api.github.com/users/nwalters512/gists{/gist_id}","starred_url":"https://api.github.com/users/nwalters512/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nwalters512/subscriptions","organizations_url":"https://api.github.com/users/nwalters512/orgs","repos_url":"https://api.github.com/users/nwalters512/repos","events_url":"https://api.github.com/users/nwalters512/events{/privacy}","received_events_url":"https://api.github.com/users/nwalters512/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-05T18:12:02Z","updated_at":"2018-02-05T18:12:02Z","closed_at":null,"author_association":"NONE","body":"## What are you doing?\r\nFollowing the advice in #6229, I addeda `defaultScope` to exclude the created/updated timestamps from all queries by default. Below is a minimal case that reproduces the issue I'm seeing.\r\n\r\n```js\r\nconst Sequelize = require('sequelize')\r\n\r\nconst sequelize = new Sequelize({\r\n  dialect: 'sqlite',\r\n  storage: './demo.sqlite',\r\n  logging: true,\r\n  define: {\r\n    defaultScope: {\r\n      attributes: {\r\n        exclude: ['createdAt', 'updatedAt'],\r\n      },\r\n    },\r\n  },\r\n})\r\n\r\nconst User = sequelize.define('user', {\r\n  name: Sequelize.STRING,\r\n})\r\n\r\nconst Course = sequelize.define('course', {\r\n  name: Sequelize.STRING,\r\n})\r\n\r\nUser.belongsToMany(Course, { as: 'staffAssignments', through: 'staffAssignment' })\r\nCourse.belongsToMany(User, { as: 'staff', through: 'staffAssignment' })\r\n\r\nsequelize.sync({ force: true }).then(() => {\r\n  User.findOrCreate({ where: { name: 'test1' } }).spread((user) => {\r\n    Course.findOrCreate({ where: { name: 'test2' } }).spread((course) => {\r\n      // Fails with `Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: id`\r\n      user.addStaffAssignment(course)\r\n    })\r\n  })\r\n})\r\n```\r\n\r\n## What do you expect to happen?\r\nAn appropriate entry linking my `course` and `user` should be created in the `staffAssignment` table.\r\n\r\n## What is actually happening?\r\nSequelize attempts to query the `staffAssignment` for the non-existent `id` column\r\n\r\n```\r\nExecuting (default): SELECT `id` FROM `staffAssignment` AS `staffAssignment` WHERE `staffAssignment`.`userId` = 1 AND `staffAssignment`.`courseId` IN (1);\r\nUnhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: id\r\n    at Query.formatError (/Users/nathan/git/queue/node_modules/sequelize/lib/dialects/sqlite/query.js:423:16)\r\n    at afterExecute (/Users/nathan/git/queue/node_modules/sequelize/lib/dialects/sqlite/query.js:119:32)\r\n    at replacement (/Users/nathan/git/queue/node_modules/sqlite3/lib/trace.js:19:31)\r\n    at Statement.errBack (/Users/nathan/git/queue/node_modules/sqlite3/lib/sqlite3.js:16:21)\r\n```\r\n\r\nWhen I remove the default scope, Sequelize goes back to querying `staffAssignment` correctly:\r\n\r\n```\r\nExecuting (default): SELECT `createdAt`, `updatedAt`, `userId`, `courseId` FROM `staffAssignment` AS `staffAssignment` WHERE `staffAssignment`.`userId` = 1 AND `staffAssignment`.`courseId` IN (1);\r\nExecuting (default): INSERT INTO `staffAssignment` (`createdAt`,`updatedAt`,`userId`,`courseId`) VALUES ('2018-02-05 18:10:31.049 +00:00','2018-02-05 18:10:31.049 +00:00',1,1);\r\n```\r\n\r\n\r\n__Dialect:__ sqlite\r\n__Dialect version:__ sqlite3@3.1.13\r\n__Database version:__ 3.16.2\r\n__Sequelize version:__ 4.32.6\r\n__Tested with latest release:__ Yes\r\n"}}
{"actor":{"display_login":"nwalters512"}}