{"action":"created","issue":{"url":"https://api.github.com/repos/jinzhu/gorm/issues/806","labels_url":"https://api.github.com/repos/jinzhu/gorm/issues/806/labels{/name}","comments_url":"https://api.github.com/repos/jinzhu/gorm/issues/806/comments","events_url":"https://api.github.com/repos/jinzhu/gorm/issues/806/events","html_url":"https://github.com/jinzhu/gorm/issues/806","id":127174073,"number":806,"title":"Cannot catch duplicate primary key error for join table","user":{"login":"hit9","id":1687443,"avatar_url":"https://avatars.githubusercontent.com/u/1687443?v=3","gravatar_id":"","url":"https://api.github.com/users/hit9","html_url":"https://github.com/hit9","followers_url":"https://api.github.com/users/hit9/followers","following_url":"https://api.github.com/users/hit9/following{/other_user}","gists_url":"https://api.github.com/users/hit9/gists{/gist_id}","starred_url":"https://api.github.com/users/hit9/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hit9/subscriptions","organizations_url":"https://api.github.com/users/hit9/orgs","repos_url":"https://api.github.com/users/hit9/repos","events_url":"https://api.github.com/users/hit9/events{/privacy}","received_events_url":"https://api.github.com/users/hit9/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-18T08:08:42Z","updated_at":"2016-01-18T09:12:21Z","closed_at":"2016-01-18T09:12:21Z","body":"Hello, I cannot catch the UNIQUE constraint failure error.\r\n\r\nTest code is here:\r\n\r\n```go\r\npackage main\r\n\r\nimport (\r\n        \"github.com/jinzhu/gorm\"\r\n        _ \"github.com/mattn/go-sqlite3\"\r\n)\r\n\r\ntype User struct {\r\n        ID       int       `gorm:\"primary_key\"`\r\n        Projects []Project `gorm:\"many2many:project_users\"`\r\n}\r\n\r\ntype Project struct {\r\n        ID    int    `gorm:\"primary_key\"`\r\n        Users []User `gorm:\"many2many:project_users\"`\r\n}\r\n\r\nfunc main() {\r\n        db, _ := gorm.Open(\"sqlite3\", \"test.db\")\r\n        defer db.Close()\r\n        db.AutoMigrate(&User{}, &Project{})\r\n        proj := Project{ID: 2}\r\n        err := db.Debug().Model(&proj).Association(\"Users\").Append(User{ID: 1}).Error\r\n        if err != nil {\r\n                // Should ok.\r\n                panic(err)\r\n        }\r\n        // Add again. \r\n        err = db.Debug().Model(&proj).Association(\"Users\").Append(User{ID: 1}).Error\r\n        if err != nil {\r\n                // Should UNIQUE constraint failed, but not\r\n                panic(err)\r\n        }\r\n}\r\n```\r\n\r\nThe SQL from gorm  debug is:\r\n\r\n```sql\r\nINSERT INTO \"project_users\" (\"project_id\",\"user_id\") SELECT '2','1'  WHERE NOT EXISTS (SELECT * FROM \"project_users\" WHERE \"project_id\" = '2' A\r\nND \"user_id\" = '1')\r\n```\r\n\r\nBut why gorm adds the  `where not exists` to the insert statement? Thanks"},"comment":{"url":"https://api.github.com/repos/jinzhu/gorm/issues/comments/172470951","html_url":"https://github.com/jinzhu/gorm/issues/806#issuecomment-172470951","issue_url":"https://api.github.com/repos/jinzhu/gorm/issues/806","id":172470951,"user":{"login":"jinzhu","id":6843,"avatar_url":"https://avatars.githubusercontent.com/u/6843?v=3","gravatar_id":"","url":"https://api.github.com/users/jinzhu","html_url":"https://github.com/jinzhu","followers_url":"https://api.github.com/users/jinzhu/followers","following_url":"https://api.github.com/users/jinzhu/following{/other_user}","gists_url":"https://api.github.com/users/jinzhu/gists{/gist_id}","starred_url":"https://api.github.com/users/jinzhu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jinzhu/subscriptions","organizations_url":"https://api.github.com/users/jinzhu/orgs","repos_url":"https://api.github.com/users/jinzhu/repos","events_url":"https://api.github.com/users/jinzhu/events{/privacy}","received_events_url":"https://api.github.com/users/jinzhu/received_events","type":"User","site_admin":false},"created_at":"2016-01-18T09:12:21Z","updated_at":"2016-01-18T09:12:21Z","body":"Hi @hit9 \r\n\r\nWhy error? it is better to just works?\r\n\r\n---\r\nBtw, if you really want to the error, you could overwrite the join table handler,  refer https://github.com/jinzhu/gorm/blob/master/join_table_handler.go#L105-L114"}}