{"action":"created","issue":{"url":"https://api.github.com/repos/jinzhu/gorm/issues/653","labels_url":"https://api.github.com/repos/jinzhu/gorm/issues/653/labels{/name}","comments_url":"https://api.github.com/repos/jinzhu/gorm/issues/653/comments","events_url":"https://api.github.com/repos/jinzhu/gorm/issues/653/events","html_url":"https://github.com/jinzhu/gorm/issues/653","id":105471014,"number":653,"title":"Self referencing many2many relation","user":{"login":"acristal","id":6374083,"avatar_url":"https://avatars.githubusercontent.com/u/6374083?v=3","gravatar_id":"","url":"https://api.github.com/users/acristal","html_url":"https://github.com/acristal","followers_url":"https://api.github.com/users/acristal/followers","following_url":"https://api.github.com/users/acristal/following{/other_user}","gists_url":"https://api.github.com/users/acristal/gists{/gist_id}","starred_url":"https://api.github.com/users/acristal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acristal/subscriptions","organizations_url":"https://api.github.com/users/acristal/orgs","repos_url":"https://api.github.com/users/acristal/repos","events_url":"https://api.github.com/users/acristal/events{/privacy}","received_events_url":"https://api.github.com/users/acristal/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/jinzhu/gorm/labels/%23query","name":"#query","color":"fbca04"},{"url":"https://api.github.com/repos/jinzhu/gorm/labels/%23relation","name":"#relation","color":"fbca04"},{"url":"https://api.github.com/repos/jinzhu/gorm/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-08T21:18:12Z","updated_at":"2015-11-01T23:23:25Z","closed_at":null,"body":"Hi guys,\r\n\r\nI am stuck trying to make a self referencing many2many relationship with gorm.\r\n\r\nI first tried the naive approach thinking that it should work:\r\n```go\r\ntype User struct {\r\n    Id int64\r\n    Contacts []User `gorm:\"many2many:user_contacts;\"`\r\n}\r\n```\r\nBut I get errors saying that the column `user_id` is duplicated.\r\n\r\nI found a post saying that this:\r\n```go\r\ntype User struct {\r\n    Id int64\r\n    Contacts []User `gorm:\"foreignkey:user_id;associationforeignkey:contact_id;many2many:user_contacts;\"`\r\n}\r\n```\r\nshould work. However, it doesn't because it tries to create a join table with no column.\r\nI believe it comes from a29230c86f0849b51bbb3c19b71f108fbb74541d, where you check that the foreign key fields exist with `toScope.FieldByName` in `model_struct.go`, so it is not possible to use a non-existent field in `foreignkey` and `associationforeignkey`.\r\n\r\nI however found a way to bypass that by using a custom JoinTableHandler (following the example in `join_table_test.go`):\r\nI use this struct:\r\n```go\r\ntype User struct {\r\n    Id int64\r\n    Contacts []User `gorm:\"associationforeignkey:nonExistentField;many2many:user_contacts;\"`\r\n}\r\n```\r\nIt works because the `associationForeignKey` doesn't exist, so it creates the join table `user_contacts` with the fields `user_id` and nothing else. Then, when migrating, it adds the `contact_id` column.\r\n\r\nSo in the end, I have my join table with the good columns, but I don't think this was the intended way to do it.\r\nAm I doing something wrong?\r\n\r\nThanks a lot,"},"comment":{"url":"https://api.github.com/repos/jinzhu/gorm/issues/comments/152874921","html_url":"https://github.com/jinzhu/gorm/issues/653#issuecomment-152874921","issue_url":"https://api.github.com/repos/jinzhu/gorm/issues/653","id":152874921,"user":{"login":"DieselDraft","id":1092854,"avatar_url":"https://avatars.githubusercontent.com/u/1092854?v=3","gravatar_id":"","url":"https://api.github.com/users/DieselDraft","html_url":"https://github.com/DieselDraft","followers_url":"https://api.github.com/users/DieselDraft/followers","following_url":"https://api.github.com/users/DieselDraft/following{/other_user}","gists_url":"https://api.github.com/users/DieselDraft/gists{/gist_id}","starred_url":"https://api.github.com/users/DieselDraft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DieselDraft/subscriptions","organizations_url":"https://api.github.com/users/DieselDraft/orgs","repos_url":"https://api.github.com/users/DieselDraft/repos","events_url":"https://api.github.com/users/DieselDraft/events{/privacy}","received_events_url":"https://api.github.com/users/DieselDraft/received_events","type":"User","site_admin":false},"created_at":"2015-11-01T23:23:25Z","updated_at":"2015-11-01T23:23:25Z","body":"same problem"}}