Gorm:外键约束(属于)
我是Gorm的新手,我得到了以下错误。当我要插入记录时。我尝试了很多方法,但都无法解决它,我也用谷歌搜索了一下,但都找不到。错误是Gorm:外键约束(属于),go,go-gorm,Go,Go Gorm,我是Gorm的新手,我得到了以下错误。当我要插入记录时。我尝试了很多方法,但都无法解决它,我也用谷歌搜索了一下,但都找不到。错误是pq:insert或update on table“user\u info”违反外键约束“user\u info\u country\u code\u fkey”,如何解决此问题,我添加了我的实体并查询了我在此处遗漏的内容,提前感谢 实体 type User struct { gorm.Model CountryCode string
pq:insert或update on table“user\u info”违反外键约束“user\u info\u country\u code\u fkey”
,如何解决此问题,我添加了我的实体并查询了我在此处遗漏的内容,提前感谢
实体
type User struct {
gorm.Model
CountryCode string
Country Country `gorm:"foreignKey:CountryCode"`
FirstName string
LastName string
}
func (User) TableName() string {
return "user_info"
}
type Country struct {
gorm.Model
CountryCode string
CountryName string
ContinentName string
}
func (Country) TableName() string {
return "country_info"
}
质疑
CREATE TABLE IF NOT EXISTS "country_info" (
"id" bigserial NOT NULL,
"country_code" VARCHAR(50) UNIQUE NOT NULL,
"country_name" VARCHAR(250) UNIQUE not null,
"continent_name" VARCHAR(250) not null,
"created_at" TIMESTAMP NOT NULL,
"updated_at" TIMESTAMP NOT NULL,
"deleted_at" TIMESTAMP NULL,
PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);
CREATE TABLE IF NOT EXISTS "user_info" (
"id" bigserial NOT NULL,
"country_code" VARCHAR(50) REFERENCES country_info ("country_code"),
"first_name" VARCHAR(50) NOT NULL,
"last_name" VARCHAR(50) NOT NULL,
"created_at" TIMESTAMP NOT NULL,
"updated_at" TIMESTAMP NOT NULL,
"deleted_at" TIMESTAMP NULL,
PRIMARY KEY ("id")
)WITH (
OIDS=FALSE
);