GORM Playground Link
https://github.com/go-gorm/playground/pull/539
Description
When I use AutoMigrate a struct with Belongs to relationship with another struct, it will always ALTER TABLE when I restart the application.
type Company struct {
ID int
Name string
}
Table Definition:
CREATE TABLE "companies" ("id" bigserial,"name" text,PRIMARY KEY ("id"))
type User struct {
gorm.Model
Name string
CompanyID int
Company Company
}
Table Definition:
CREATE TABLE "users" ("id" bigserial,"created_at" timestamptz,"updated_at" timestamptz,"deleted_at" timestamptz,"name" text,"company_id" bigint,PRIMARY KEY ("id"),CONSTRAINT "fk_users_company" FOREIGN KEY ("company_id") REFERENCES "companies"("id"))
This is so far correct.
Now when I kill my application and attempt to rerun with auto migration as one of the start up process. I see the following in the logs:
ALTER TABLE "users" ALTER COLUMN "company_id" TYPE bigint
This happens every time I restart the application. We have 15 structs with belongs to relationship among each other. This is issuing ~29 similar alter queries thats locking the DB instance when the clients attempt to read the data.
GORM.version: v1.23.8 GORM.driver.postgres.version: v1.3.9
Expected
When the schemas are applied, the alter statements should not be issued.
Comment From: a631807682
Upgrade gorm and postgres version. This issue has been fixed https://github.com/go-gorm/gorm/commit/62593cfad03ebf1e6cae30bac010655b4a28ff67
Comment From: Ravikanth143143
Thanks @a631807682. That worked.