复合唯一键的 Gorm 标签
Gorm tags for compound unique key
如何描述复合 UNIQUE 键的 gorm 标签?
Postgres:
phone_country_code VARCHAR(10),
phone_number VARCHAR(25),
CONSTRAINT phone_country_code_number UNIQUE (phone_country_code, phone_number)
Go(模型结构的字段):
PhoneCountryCode string `gorm:"column:phone_country_code; type:VARCHAR(10)"`
PhoneNumber string `gorm:"column:phone_number; type:VARCHAR(25)"`
根据docs on Composite Indexes,您可以通过在两个或多个字段中使用相同的索引名称来创建唯一的复合索引:
PhoneCountryCode string `gorm:"uniqueIndex:idx_code_phone; type:VARCHAR(10)"`
PhoneNumber string `gorm:"uniqueIndex:idx_code_phone; type:VARCHAR(25)"`
这与添加约束具有相同的效果。
(注意:为了易读性,我删除了 column
标签,不需要它,因为 gorm snake 默认情况下会使用字段名称来获取列名称)。
如何描述复合 UNIQUE 键的 gorm 标签?
Postgres:
phone_country_code VARCHAR(10),
phone_number VARCHAR(25),
CONSTRAINT phone_country_code_number UNIQUE (phone_country_code, phone_number)
Go(模型结构的字段):
PhoneCountryCode string `gorm:"column:phone_country_code; type:VARCHAR(10)"`
PhoneNumber string `gorm:"column:phone_number; type:VARCHAR(25)"`
根据docs on Composite Indexes,您可以通过在两个或多个字段中使用相同的索引名称来创建唯一的复合索引:
PhoneCountryCode string `gorm:"uniqueIndex:idx_code_phone; type:VARCHAR(10)"`
PhoneNumber string `gorm:"uniqueIndex:idx_code_phone; type:VARCHAR(25)"`
这与添加约束具有相同的效果。
(注意:为了易读性,我删除了 column
标签,不需要它,因为 gorm snake 默认情况下会使用字段名称来获取列名称)。