Postgresql 为什么GORM没有';是否为博士后生成外部约束?
我是Go和GORM的新手。在生成用于测试的DB之后,我没有看到一对多关系在Postgres中得到正确表示 我已经创建了两个要迁移到Postgres的模型,例如,这两个模型:Postgresql 为什么GORM没有';是否为博士后生成外部约束?,postgresql,go,go-gorm,Postgresql,Go,Go Gorm,我是Go和GORM的新手。在生成用于测试的DB之后,我没有看到一对多关系在Postgres中得到正确表示 我已经创建了两个要迁移到Postgres的模型,例如,这两个模型: type PgPiece struct { ID string `gorm:"primary_key"` Name string MinPrice float64 Likes int Description
type PgPiece struct {
ID string `gorm:"primary_key"`
Name string
MinPrice float64
Likes int
Description string
Materials string
Techniques string
Width float64
Height float64
Length float64
Hours int
CreatedAt *time.Time
ModifiedAt *time.Time
IsFavorite bool
EstimatedValue float64
Owner PgUser `gorm:"foreignkey:OwnerID"`
OwnerID string
Author PgUser `gorm:"foreignkey:AuthorID"`
AuthorID string
Favorites []PgUser `gorm:"many2many:user_favorite_piece"`
Images []PgPieceImage `gorm:"foreignkey:piece_id"`
}
因此,我在PgPieceImage的postgres中看到了这一点
CREATE TABLE public.piece_image
(
id text COLLATE pg_catalog."default" NOT NULL,
url text COLLATE pg_catalog."default",
"position" integer,
width integer,
height integer,
created_at timestamp with time zone,
piece_id text COLLATE pg_catalog."default",
CONSTRAINT piece_image_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.piece_image
OWNER to freddy;
为什么会有非外来约束?我会问:为什么会有外来约束?您使用自动迁移了吗?你加了另一张桌子了吗@是的。我添加了自动迁移,两个表都在那里。数据库是用每个表创建的。对于我的外部约束,是的,我希望在某种程度上看到这种约束。现在的情况是,如果我们只检查AutoMigrate调用生成的sql,它们只是表,彼此之间没有任何关系。我想知道我是做错了什么,还是GORM没有正确地生成外部约束。如果可能的话,请告诉我。当做
CREATE TABLE public.piece_image
(
id text COLLATE pg_catalog."default" NOT NULL,
url text COLLATE pg_catalog."default",
"position" integer,
width integer,
height integer,
created_at timestamp with time zone,
piece_id text COLLATE pg_catalog."default",
CONSTRAINT piece_image_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.piece_image
OWNER to freddy;