强制GORM在自动迁移到PostgreSQL时使用特定的整数类型

huangapple go评论92阅读模式
英文:

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

问题

我的Go模型如下:

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
}

当使用上述模型运行AutoMigrate()时,我希望pSQL数据库中的impressionsclicks列的类型为integer。然而,即使使用了那些gorm标签,它们仍然以int4类型结束。我尝试过手动使用上述标签的int2 int4 int8,它们都按预期工作。此外,当我尝试使用int标签时,它们被强制转换为int8。如何修复这个行为并在pSQL中获得integer类型?

编辑:我正在使用DBeaver查看数据库。

英文:

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.

答案1

得分: 2

根据PostgreSQL文档的说明:

SQL只指定了整数类型integer(或int),smallint和bigint。
类型名称int2,int4和int8是扩展,也被其他一些SQL数据库系统使用。

你可以使用intintegersmallintbigint,其他的都只是别名。

英文:

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.

huangapple
  • 本文由 发表于 2022年8月10日 11:23:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/73300272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定