一个是创建表时建索引?
一个是插入记录时的验证?
"unique_index" 和 "unique" 区别能否说一下
------------------------------验证结果--------------------------------
环境:mysql
方式:使用自动迁移生成的表
代码:
TestUnique1 string `gorm:"size:255;unique"`
TestUnique2 string `gorm:"size:255;unique_index"`
结果:只是索引名字的格式不同,多了uix_user2_ 前缀
test_unique1
uix_user2_test_unique2
还有,在 gorm.Model 里,ID 是 uint
uint的实际位数和系统有关吧,以mysql举例
32位系统创建出来的字段是 int
32位系统创建出来的字段是 bigint?
这个我去验证一下
------------------------------验证结果--------------------------------
方式:使用自动迁移生成的表
验证:gorm.Model
结果:
3个时间戳字段全部位于最前方
uint 在64位win10的mysql里,生成了int型
type User2 struct {
Name string
Age sql.NullInt64
Birthday *time.Time
Email string `gorm:"type:varchar(100);unique_index"`
Role string `gorm:"size:255"` // set field size to 255
MemberNumber *string `gorm:"unique;not null"` // set member number to unique and not null
Num int `gorm:"AUTO_INCREMENT"` // set num to auto incrementable
Address string `gorm:"index:addr"` // create index with name `addr` for address
IgnoreMe int `gorm:"-"` // ignore this field
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
这段官方示例中,类型有指针型,这个不影响迁移,但是有什么区别?
使用上有区别吗
如果我想为mysql的单个字段指定字符集
可以这样吗?
Comment string `gorm:"set utf8mb4"`
------------------------------验证结果--------------------------------
方式:使用自动迁移生成的表
结果:不起作用,新的comment 还是 utf8
unique means "has to be unique value", it's an equivalent of setting a UNIQUE constraint on a database table field.
unique_index means "index database on this value, which is unique", it's an equivalent of creating a unique index in the database — you can specify a name of such index and use the annotation with the same name on multiple struct fields:
// just an exampe
type Example struct {
// two people can have the same name
Name string `gorm:"primary_key;unique_index:exampleindex"`
// but usernames have to be unique
Username string `gorm:"unique;"`
// colors can be repeating too
FavoriteColor string `gorm:"unique_index:exampleindex"`
// But combinations of User + FavoriteColor have to be UNIQUE
}
@ermik In MySQL, both via unique_index?
TestUnique1 string `gorm:"size:255;unique"`
TestUnique2 string `gorm:"size:255;unique_index"`
result:both created unique index, test2 have more prefix than test1
test_unique1
uix_user2_test_unique2
The default unique index creation for UNIQUE field is a ?bug?. I don't know if it's MySQL creating index for UNIQUE fields by default, or if it is gorm doing it.
It won't make your database slower...
To decide which option to use — think of the data you have and the _constraints_ you place on it as in example above.
@ermik How to change a field's charset to utf8mb4?
gorm:"set utf8mb4" can't work
DeletedAt *time.Time
In some cases if you want to set nil to a column(for example: end_time), you should define (EndTime * time.Time) in struct.
@fanybook try calling db.Set("gorm:table_options", "charset=utf8mb4")
@cjun714 you can define your own Model struct and embed it on your types.
db.Set("gorm:table_options", "charset=utf8mb4") can set all varchar and text fields to utf8mb4
set utf8mb4 Only work on a single field
This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you
Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog
Most helpful comment
uniquemeans "has to be unique value", it's an equivalent of setting a UNIQUE constraint on a database table field.unique_indexmeans "index database on this value, which is unique", it's an equivalent of creating a unique index in the database — you can specify a name of such index and use the annotation with the same name on multiple struct fields: