{"action":"opened","issue":{"url":"https://api.github.com/repos/jinzhu/gorm/issues/719","labels_url":"https://api.github.com/repos/jinzhu/gorm/issues/719/labels{/name}","comments_url":"https://api.github.com/repos/jinzhu/gorm/issues/719/comments","events_url":"https://api.github.com/repos/jinzhu/gorm/issues/719/events","html_url":"https://github.com/jinzhu/gorm/issues/719","id":116160125,"number":719,"title":"Many-to-Many and attributes","user":{"login":"Quentin-M","id":1332289,"avatar_url":"https://avatars.githubusercontent.com/u/1332289?v=3","gravatar_id":"","url":"https://api.github.com/users/Quentin-M","html_url":"https://github.com/Quentin-M","followers_url":"https://api.github.com/users/Quentin-M/followers","following_url":"https://api.github.com/users/Quentin-M/following{/other_user}","gists_url":"https://api.github.com/users/Quentin-M/gists{/gist_id}","starred_url":"https://api.github.com/users/Quentin-M/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Quentin-M/subscriptions","organizations_url":"https://api.github.com/users/Quentin-M/orgs","repos_url":"https://api.github.com/users/Quentin-M/repos","events_url":"https://api.github.com/users/Quentin-M/events{/privacy}","received_events_url":"https://api.github.com/users/Quentin-M/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-11-10T17:36:41Z","updated_at":"2015-11-10T17:36:41Z","closed_at":null,"body":"Hi !\r\n\r\nI was wondering what would be the best way to create a many-to-many relationship with extra attributes ? For instance, I'd like to have `Library <--> LibraryHasBook (count) <--> Books`. LibraryHasBook would have the following fields `(library_id, book_id, count)`. Has anyone done or seen that pattern before ?\r\n\r\nI was thinking of:\r\n```\r\ntype Library struct {\r\n\tID            uint   `gorm:\"primary_key\"`\r\n\tName      string\r\n\tBooks     []LibraryHasBook\r\n}\r\n\r\ntype LibraryHasBook struct {\r\n\tLibrary   Library   `gorm:\"primary_key\"`\r\n\tBook Book `gorm:\"primary_key\"`\r\n\tCount   string\r\n}\r\n\r\ntype Book struct {\r\n\tID            uint   `gorm:\"primary_key\"`\r\n\tName      string\r\n\tLibraries  []LibraryHasBook\r\n}\r\n```\r\n\r\nAnd then inserting a new Book & associated Libraries with something like:\r\n```\r\nbook := Book{\r\n\tName: \"The Lords of the Ring\",\r\n\tLibraries: []LibraryHasBook {\r\n\t\tLibraryHasBook{\r\n\t\t\tLibrary: Library{Name: \"Tolkien Specialists},\r\n\t\t},\r\n\t\tLibraryHasBook{\r\n\t\t\tLibrary: Library{Name: \"Awesome Library},\r\n\t\t},\r\n\t},\r\n}\r\ndb.Create(&book)\r\n```\r\n\r\nBut I am quite not sure if it would work (the Go is most likely not valid anyways as I typed it directly in that issue), especially if GORM would be able to fill LibraryHasBook's Library, and what annotation should be used, etc.\r\n\r\nI did not find anything related in the docs, pardon me if I am blind.\r\nThank you so much."}}