英文:
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数据库中的impressions
和clicks
列的类型为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数据库系统使用。
你可以使用int
,integer
,smallint
和bigint
,其他的都只是别名。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论