Reputation: 184
I'm working on an application that stores mountains by region.
type Mountain struct {
Name string
Height int
RegionID int
}
type Region struct {
ID int64
Name string
Mountains []Mountain
}
As you may see, I have an array of mountains with a foreign key constraint on RegionID
. I'm trying on purpose to avoid setting ID on the mountains, in order to have them fully replaced when updating.
When I try to save the region:
var region = &models.Region{
Name: "Lombardia",
Mountains: []models.Mountain{
{
Name: "Pizzo Coca",
Height: 3050,
},
{
Name: "Bernina",
Height: 4049,
},
},
}
db.Create(region)
I have this error:
ERRORE: ON CONFLICT DO UPDATE richiede una specifica di inferenza o il nome di un vincolo
(SQLSTATE 42601)
I know that is not in english, but I can't find the english version on the net.
Upvotes: 1
Views: 140
Reputation: 39430
Try using the Upsert / On Conflict, as example:
import "gorm.io/gorm/clause"
// Do nothing on conflict
db.Clauses(clause.OnConflict{DoNothing: true}).Create(&user)
Some info in this article
Upvotes: 1