GORM Playground Link

⚠️ Go playground has CGO Disabled, hence you cannot run code importing the SQLITE Driver. I made a really easy to read and documented Minimum Reproducible Example so that you don't waste more than 2 min setting up a local test environment on your machine.

Description

Contextual information:

  • Go Version: 1.20
  • GORM Version: gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
  • DB Driver: gorm.io/driver/sqlite v1.5.2

This seems to be a follow up on this old issue as i get the exact same behavior.

I have a User struct and a UserPermission struct: ``go type User struct { UserID stringgorm:"column:user_id;primaryKey" json:"user_id"` }

type UserPermission struct { ID uint gorm:"primaryKey;autoIncrement" json:"-" UserID string gorm:"type:text;column:user_id;not null;index:idx_tenant_tech;constraint:OnDelete:CASCADE" json:"user_id" User User gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" Role Role gorm:"column:role;not null" json:"role" } ``` If i migrate theUserstruct only all is well and good, user_id is of typetext`:

CREATE TABLE "users" ("user_id" text,PRIMARY KEY ("user_id"))

BUT ! if i start migrating the UserPermission struct alongside it's User counterpart then of all a sudden the User table has it's user_id type set to integer:

CREATE TABLE "users" ("user_id" integer,PRIMARY KEY ("user_id"),CONSTRAINT "fk_user_permissions_user" FOREIGN KEY ("user_id") REFERENCES "user_permissions"("id") ON DELETE CASCADE).

I also tried "hardcoding" the type by explicitly setting the type:text or even type:varchar(255). tag on the field but to no avail.

Comment From: DoppleDankster

Will reopen with the proper GORM Playground. i read it as Go Playground .

Comment From: DoppleDankster

I created the pull request, this issue is now re-opened

Comment From: jinzhu

For foreign key fields, it should use the same data type as the referred field, so you can change the referred field's type.