gorm 与 foreignKey 引用有很多关系
gorm has many relation with foreignKey reference
我的目标是存档与 gorm 的“有很多”关系
我不想生成任何 ID,所以我故意没有在我的结构中使用 gorm.Model
我将两个结构设置为:
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 应该是 Applications
的外键
(Translation.ApplicationName + Translation.Locale) Translations
的主键
创建应用程序后
{
"name" : "postedApplication1",
"description" : "postedDescription3",
"titles" : [
{
"locale": "de-DE",
"value":"deutsch"
},
{
"locale": "de-AT",
"value":"AT"
}
]
}
我收到以下错误
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 SET
application_name
=excluded
.application_name
和
ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint
[0.531ms] [rows:0] UPDATE applications
SET
description
="postedDescription3" WHERE name
= "postedApplication1"
[GIN] 2021/08/27 - 11:23:00 | 200 | 841.953µs | 127.0.0.1 |
POST "/applications"
有人知道我做错了什么
已解决 qx.X,p
一切正确!
安装 vscode-sqlite 并检查数据库后,我发现 sqlite 表与我设计的不一样
问题是 AutoMigrate 由于在开发过程中发生了很多变化而产生了无效状态
database.AutoMigrate(&models.Application{}, &models.Translation{})
我必须删除 sqlite“gorm.db”文件并重新启动应用程序
我的目标是存档与 gorm 的“有很多”关系
我不想生成任何 ID,所以我故意没有在我的结构中使用 gorm.Model
我将两个结构设置为:
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 应该是 Applications
的外键(Translation.ApplicationName + Translation.Locale) Translations
的主键创建应用程序后
{
"name" : "postedApplication1",
"description" : "postedDescription3",
"titles" : [
{
"locale": "de-DE",
"value":"deutsch"
},
{
"locale": "de-AT",
"value":"AT"
}
]
}
我收到以下错误
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
和
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"
有人知道我做错了什么
已解决 qx.X,p
一切正确!
安装 vscode-sqlite 并检查数据库后,我发现 sqlite 表与我设计的不一样
问题是 AutoMigrate 由于在开发过程中发生了很多变化而产生了无效状态
database.AutoMigrate(&models.Application{}, &models.Translation{})
我必须删除 sqlite“gorm.db”文件并重新启动应用程序