Your Question
Hi thanks for the great library!
I've looked for a while for the same question but haven't found anything. Is there a way to select a specific association instead of upserting when running Create on an associative model. I would like to make sure that gorm returns an error if it can't find the relevant foreign key rather than creating a new row.
The document you expected this should be explained
user := User{
Name: "jinzhu",
Language: Language{Name: "ZH"},
}
db.Create(&user)
// BEGIN TRANSACTION;
// INSERT INTO "languages" ("name") VALUES ('ZH') ON CONFLICT DO NOTHING RETURNING "id";
// INSERT INTO "users" (name,language_id) VALUES ("jinzhu", 1);
// COMMIT;
Expected answer
user := User{
Name: "jinzhu",
Language: Language{Name: "ZH"},
}
db.Create(&user)
// BEGIN TRANSACTION;
// SELECT "id" FROM "languages" WHERE "name" = "ZH";
// INSERT INTO "users" (name,language_id) VALUES ("jinzhu", 1);
// COMMIT;
Comment From: jinzhu
Maybe Omit creating association and check it exists in the hooks or callbacks?