Home > other >  Force GORM to use specific INTEGER type when auto-migrating to PostgreSQL
Force GORM to use specific INTEGER type when auto-migrating to PostgreSQL

Time:08-10

My model in Go is:

type Sales_Daily_db struct {
    Nation_shipping string
    Date             datatypes.Date
    Impressions      int `gorm:"type:integer;"`
    Clicks           int `gorm:"type:integer;"`
    Cost             float32
    ATB              float32
    OKL              float32
}

When running AutoMigrate() using the above model, I want the impressions and clicks columns in pSQL database to be of type integer. However, even with those gorm tags, they still ended up as type int4. I have tried int2 int4 int8 manually with the tags above, and they all worked accordingly. Additionally, when I try int tag, they are forced into int8. How to fix this behavior and get integer type specifially in pSQL?

Edit: I am using DBeaver to look at the database.

CodePudding user response:

According to PostgreSQL documentation, this is stated:

SQL only specifies the integer types integer (or int), smallint, and bigint. The type names int2, int4, and int8 are extensions, which are also used by some other SQL database systems.

You should be fine with int, integer, smallint and bigint. Anything beyond that is just aliases.

  • Related