【问题标题】:Gorm: How to set unsigned interger in tagsGorm:如何在标签中设置无符号整数
【发布时间】:2023-02-15 17:46:57
【问题描述】:

我有以下结构,当我运行 AutoMigration 时,我希望在表字段数量 quantity 上设置无符号,但事实并非如此。

这是我使用的结构

type Item struct {
    ID           int        `json:"id" gorm:"primaryKey"`
    Quantity     int        `json:"quantity" gorm:"type:integer;not null;unsigned;"`

}

生成的 SQL 没有 UNSIGNED

    "id" BIGINT NOT NULL DEFAULT 'nextval(''items_id_seq''::regclass)',
    "quantity" INTEGER NOT NULL,

如何使用 gorm 和 postgres 制作未签名的作品?任何帮助将不胜感激。

【问题讨论】:

  • 试试数据类型uint
  • @NirajKumar 它没有用
  • 是的,它不适用于int。您可以尝试运行 sql 查询将表更改为 int4,然后运行迁移脚本。另外,试试float32。似乎如果该列已经存在,则迁移只能更改具有某些数据类型的列。这有效int4 -> numeric (in postgres),但无法通过 gorm 迁移将其恢复为int4,可能是一些错误或必须完成以保护数据?
  • @NirajKumar 感谢您的回复,我会尝试的

标签: go-gorm


【解决方案1】:
`gorm:"type:decimal(16,2) unsigned;not null;"`

不要在数据类型和无符号之间放置分号 在 mysql 中工作,postgres 没有类型 unsigned

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2014-01-13
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多