postgres icon indicating copy to clipboard operation
postgres copied to clipboard

AutoMigrate always alters columns of "bigint"

Open tavitar opened this issue 3 years ago • 3 comments

GORM Playground Link

https://github.com/go-gorm/playground/pull/500

Description

The problem is rather simple: every run of AutoMigrate will execute a redundant ALTER COLUMN for int types (int64).

The reason?: The GORM Postgres driver uses "bigint" as the type for go's int64. However, in postgres, bigint is not the type name internally - it's just an alias to int8:

SELECT * FROM pg_type WHERE typname IN ('int8', 'bigint');

typname
int8

In practice, this means that AutoMigrate will always detect a discrepancy ("int8" != "bigint") and run an ALTER COLUMN.

The bad: You need to disable AutoMigrate by default to avoid these redundant queries. The ugly: The ALTER COLUMN int8 to bigint actually breaks conditional indexes which depend on the column and requires VACUUM ANALYZE to fix (a Postgres issue, not a problem for here, I'll report when I have time). However, suffice to say, the redundant ALTER COLUMNs are not benign.

Happy to create a pull request.

tavitar avatar Jul 22 '22 00:07 tavitar

(updated issue description with playground PR)

tavitar avatar Jul 28 '22 02:07 tavitar

I second this. I have the following:

type AllocationEntity struct {
	ID   string     `gorm:"column:id;primaryKey"`
	Name string     `gorm:"column:name"`
	Vult VultEntity `gorm:"foreignKey:ID;references:WalletID"`
	datastore.ModelWithTS
}

type VultEntity struct {
	ID       string `gorm:"column:id;primaryKey"`
	WalletID int64  `gorm:"index:wallet_id,unique;null"`
	AllocID  string `gorm:"index:alloc_id,unique"`
	datastore.ModelWithTS
}

AutoMigrate(
    &AllocationEntity{},
    &VultEntity{},
)

The strange part is in my postgresql, ID in the allocationEntity created is BigInt not string, but in the vult, it is string.

rizary avatar Aug 04 '22 09:08 rizary

related to https://github.com/go-gorm/postgres/pull/111

a631807682 avatar Aug 04 '22 16:08 a631807682

@a631807682 Hey! Do you know when this issue will be resolved? Thanks! Have a great day!

itsofirblink avatar Aug 31 '22 07:08 itsofirblink

@a631807682 Hey! Do you know when this issue will be resolved? Thanks! Have a great day!

I don't know, it is waiting for the maintainer review.

a631807682 avatar Aug 31 '22 07:08 a631807682

@a631807682 Hey! Do you know when this issue will be resolved? Thanks! Have a great day!

I don't know, it is waiting for the maintainer review.

No problem, i see that it fails on tests on some driver, that might be an issue?

itsofirblink avatar Aug 31 '22 07:08 itsofirblink

@a631807682 Hey! Do you know when this issue will be resolved? Thanks! Have a great day!

I don't know, it is waiting for the maintainer review.

No problem, i see that it fails on tests on some driver, that might be an issue?

It will fix by https://github.com/go-gorm/gorm/pull/5620

a631807682 avatar Aug 31 '22 07:08 a631807682