为什么GORM不为Postgres生成FOREIGN CONSTRAINT?

时间:2018-12-27 03:17:25

标签: postgresql go go-gorm

我是Go和GORM的新手。生成用于测试的数据库后,我看不到Postgres中可以正确表示一对多关系。

我已经创建了几个模型,可以移植到Postgres中,例如,这两个模型:

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"`

}

type PgPieceImage struct {
    ID        string `gorm:"primary_key"`
    Url       string
    Position  int
    Width     int
    Height    int
    CreatedAt *time.Time
    Piece PgPiece `gorm:"foreignkey:PieceRef"`
    PieceRef   string `gorm:"column:piece_id"`
}

结果,我在postgres中看到了PgPieceImage

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;

为什么那里没有外国约束?

0 个答案:

没有答案