Sunday, September 8, 2024
HomeGolangThe right way to apply a a number of examine constraint in...

The right way to apply a a number of examine constraint in gorm with sqlite? – Getting Assist


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” :rofl: :rofl:



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)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments