I would like to have a string-type field in a MySQL database which is case-insensitive and unique. I used the following model:
type User struct {
Id int64 `json:"id" sql:"AUTO_INCREMENT"`
Email string `json:"email" sql:"unique_index"`
}
which makes Email
unique, but
type User struct {
Id int64 `json:"id" sql:"AUTO_INCREMENT"`
Email string `json:"email" sql:"unique_index;COLLATION(utf8_general_ci)"`
}
seems to have no effect.
How can I set COLLATION
of a field to utf8_general_ci
with GORM?
You can use the sql tag in the field you want to change, like this:
type User struct {
gorm.Model
Name `sql:"type:VARCHAR(5) CHARACTER SET utf8 COLLATE utf8_general_ci"`
}