Reputation: 1292
My goal is to archive a "has many" relation with gorm
I don't want to have any generated IDs so I intentionally did not use gorm.Model in my structs
I set up my two structs like:
type Application struct {
Name string `json:"name" gorm:"primaryKey"`
Description string `json:"description"`
Translations []Translation `json:"titles" gorm:"foreignKey:ApplicationName;references:Name"`
}
type Translation struct {
ApplicationName string `json:"applicationName" gorm:"primaryKey"`
Locale string `json:"locale" gorm:"primaryKey"`
Value string `json:"value"`
}
Translation.ApplicationName should be the foreignKey to Applications
(Translation.ApplicationName + Translation.Locale) the primary key for Translations
After creating an application
{
"name" : "postedApplication1",
"description" : "postedDescription3",
"titles" : [
{
"locale": "de-DE",
"value":"deutsch"
},
{
"locale": "de-AT",
"value":"AT"
}
]
}
I got following error
ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint [0.065ms] [rows:0] INSERT INTO
translations
(application_name
,locale
,value
) VALUES ("postedApplication1","de-DE","deutsch"),("postedApplication1","de-AT","AT") ON CONFLICT (application_name
,locale
) DO UPDATE SETapplication_name
=excluded
.application_name
and
ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint [0.531ms] [rows:0] UPDATE
applications
SETdescription
="postedDescription3" WHEREname
= "postedApplication1" [GIN] 2021/08/27 - 11:23:00 | 200 | 841.953µs | 127.0.0.1 | POST "/applications"
Someone any idea what I'm doing wrong
Upvotes: 1
Views: 1065
Reputation: 1292
SOLVED qx.X,p
Everything was correct!
After I installed vscode-sqlite and inspected the database I just recognized that the sqlite tables were not like I designed them
The problem was that AutoMigrate which produced an invalid state due changing a lot during developing
database.AutoMigrate(&models.Application{}, &models.Translation{})
I had to delete sqlite "gorm.db" file and restart the application
Upvotes: 1