Principally I’ve this desk:
kind Desk struct {
Username `gorm:"column:username;kind:textual content;examine:username>=3;examine:username<=20;distinctive;not null"`
}
My aim is for Username to fulfill this situation:username >= 3 && username <= 20
However once I run this:
information := Desk{Username: "aValidUsername"}
err := db.Debug().Mannequin(&information).Save(information).Error
I get this error:constraint failed: CHECK constraint failed: chk_table_username (275)
Which makes me suppose my tag is improper. What’s the right option to do it?
I already discovered the reply::
kind Desk struct {
Username string `gorm:"column:username;kind:textual content;examine:size(username)>=3;examine:size(username)<=20;distinctive;not null"`
}
One other reply could be “Don’t use GORM in any respect”
2 Likes
Replace:
My reply was improper, as it’s the two examine circumstances work as in the event that they have been joined by the OR operator.
That is the right means:
kind Desk struct {
Username string `gorm:"column:username;kind:textual content;examine:(size(username)>=3 and size(username)<=20);distinctive;not null"`
}
if username >=3 && username <=20
== examine:(size(username)>=3 and size(username)<=20)