Your Question

How force primary key to int4 with PostgreSQL ? All Golang types are reported to int8 (bigint) : - uint - int64 - int32

Comment From: s-takehana

@perriea

type User struct {
    ID   uint `gorm:"type:int4"`
    Name string
}
2021/05/17 15:58:46 /go/pkg/mod/gorm.io/driver/postgres@v1.0.8/migrator.go:157
[16.319ms] [rows:0] CREATE TABLE "users" ("id" int4,"name" text,PRIMARY KEY ("id"))

Comment From: perriea

ah ... Thanks ! I must not have read the documentation correctly ... Sorry

Comment From: pixelfantasy

@perriea

go type User struct { ID uint `gorm:"type:int4"` Name string }

2021/05/17 15:58:46 /go/pkg/mod/gorm.io/driver/postgres@v1.0.8/migrator.go:157 [16.319ms] [rows:0] CREATE TABLE "users" ("id" int4,"name" text,PRIMARY KEY ("id"))

This also works for MariaDB and was finally the solution for me. I tried to enable two services to work on the same database. One is based on PHP Symfony and Doctrine, the other on Golang Gin Framework and GORM. While Doctrine is always using INT for primary keys, GORM tends to use BIGINT which led to the following error:

ERROR Error 1005 (HY000): Can't create table xyz (errno: 150 "Foreign key constraint is incorrectly formed")

By enforcing type:int4 now finally auto-migration works. Made my day :-)