I'm new with Go and GORM. After generating my DB for testing, I don't see that the One To Many relationship is properly represented in Postgres.
I have created a couple of models to be migrated into Postgres, for example, these 2 specifically:
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"`
}
And as result I see this in postgres for 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;
Why there is non FOREIGN CONSTRAINT there?