{"action":"closed","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/6283","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/6283/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/6283/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/6283/events","html_url":"https://github.com/sequelize/sequelize/issues/6283","id":165834093,"number":6283,"title":"Many to Many docs suggestion.","user":{"login":"savovs","id":1226564,"avatar_url":"https://avatars.githubusercontent.com/u/1226564?v=3","gravatar_id":"","url":"https://api.github.com/users/savovs","html_url":"https://github.com/savovs","followers_url":"https://api.github.com/users/savovs/followers","following_url":"https://api.github.com/users/savovs/following{/other_user}","gists_url":"https://api.github.com/users/savovs/gists{/gist_id}","starred_url":"https://api.github.com/users/savovs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/savovs/subscriptions","organizations_url":"https://api.github.com/users/savovs/orgs","repos_url":"https://api.github.com/users/savovs/repos","events_url":"https://api.github.com/users/savovs/events{/privacy}","received_events_url":"https://api.github.com/users/savovs/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-07-15T17:04:27Z","updated_at":"2016-08-01T08:12:04Z","closed_at":"2016-08-01T08:12:04Z","body":"Hello guys, I spent a week learning Sequelize. I have read the guidelines and the docs (several times) and I think a better learning experience counts as a good reason to post here.\r\n\r\nI think the Many to Many (belongs to many) associations part needs to be clarified a bit more. There are [a lot of questions](http://stackoverflow.com/search?q=sequelize+many+to+many) on Stack Overflow that all concern the same thing and most of them aren't easy to understand from people (like me) who just started learning. [This](http://stackoverflow.com/questions/22958683/how-to-implement-many-to-many-association-in-sequelize) is the best thread I've seen explaining the subject.\r\n\r\nI'm not exactly sure how to improve the docs it because I'm new, maybe a complete top-to-bottom example, not just snippets? Here's an example issue:\r\n\r\nI wanted to implement a like button. Here's what my code looks like:\r\n```javascript\r\nimport Sequelize from 'sequelize';\r\nimport Faker from 'faker';\r\n\r\nconst Connection = new Sequelize(\r\n\t'test_db',\r\n\t'postgres',\r\n\t'postgres',\r\n\t{\r\n\t\tdialect: 'postgres',\r\n\t\thost: 'localhost'\r\n\t}\r\n);\r\n\r\n// Models\r\nconst User = Connection.define('user', {\r\n\tid: {\r\n\t\ttype: Sequelize.UUID,\r\n\t\tdefaultValue: Sequelize.UUIDV4,\r\n\t\tallowNull: false,\r\n\t\tprimaryKey: true\r\n\r\n\t},\r\n\tfirstName: {\r\n\t\ttype: Sequelize.STRING,\r\n\t\tallowNull: false\r\n\t},\r\n\tlastName: {\r\n\t\ttype: Sequelize.STRING,\r\n\t\tallowNull: false\r\n\t}\r\n});\r\n\r\nconst Post = Connection.define('post', {\r\n\tid: {\r\n\t\ttype: Sequelize.UUID,\r\n\t\tdefaultValue: Sequelize.UUIDV4,\r\n\t\tallowNull: false,\r\n\t\tprimaryKey: true\r\n\t},\r\n\ttitle: {\r\n\t\ttype: Sequelize.STRING,\r\n\t\tallowNull: false\r\n\t},\r\n\tcontent: {\r\n\t\ttype: Sequelize.STRING,\r\n\t\tallowNull: false\t\t\r\n\t}\r\n});\r\n\r\n\r\n// Relationships:\r\n//\r\n// A user can create posts,\r\n// users can like those posts.\r\n\r\nUser.hasMany(Post);\r\nPost.belongsTo(User);\r\n\r\nUser.belongsToMany(Post, {\r\n    as: 'Liker',\r\n    through: 'Post_Likers',\r\n    foreignKey: 'Post_rowId'\r\n});\r\n\r\nConnection.sync({force: true}).then(() => {\r\n\r\n\t// Inserting some test data\r\n\treturn User.create({\r\n\t\tfirstName: Faker.name.firstName(),\r\n\t\tlastName: Faker.name.lastName()\r\n\t})\r\n\t.then(user => {\r\n\t\treturn user.createPost({\r\n\t\t\ttitle: 'A Post',\r\n\t\t\tcontent: 'That can be liked by users'\r\n\t\t});\t\t\r\n\t})\r\n\t// Now to like a post, testing with random user:\r\n\t.then(post => post.addUser(User.findOne()));\r\n\r\n\t// This breaks with error: \"val.replace is not a function\"\r\n\t// addUser() also breaks with the error: \"addUser is not a function\"\r\n});\r\n```\r\n\r\nI realise that my code is wrong. I just can't find a way how to fix it. Just want to let you guys know that this M:M part is a bit tricky to learn for a lot of people. This is a single example, but I tried to do the same thing with implementing anything similar to tags, where multiple users can be tagged in multiple other things. \r\n"}}
{"actor":{"display_login":"janmeier"}}