Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Constraint with Custom Error Message #6172

Open
KiddoV opened this issue Mar 21, 2023 · 0 comments
Open

Check Constraint with Custom Error Message #6172

KiddoV opened this issue Mar 21, 2023 · 0 comments
Assignees
Labels
type:question general questions

Comments

@KiddoV
Copy link

KiddoV commented Mar 21, 2023

Your Question

Supposed I have a model:

type Issue struct {
    gorm.Model
    UserLevel string `gorm:"type:TINYINT;not null;DEFAULT:0;check:user_level <= 2" json:"userLevel"`
    PriorityLevel uint    `gorm:"type:TINYINT;not null;DEFAULT:0;check:priority_level <= 5" json:"priorityLevel"`
}

When a check constraint failed, I only see the this message every time:

....Check constraint 'chk__<column_name>' is violated.

How can I add a custom check constraint error message when a check is failed?

So I want...
If check on `UserLevel` failed: I want to see: Check constraint 'chk__<userLevel>' failed: user level should be from 0 to 2
Or
If check on `PriorityLevel` failed: I want to see: Check constraint 'chk__<priorityLevel>' failed: priority level should be from 0 to 5

In the normal SQL (MySQL) specifically, I think we can do this:

ADD CONSTRAINT 
[Foo cannot be greater than Bar. Please be sure to check your foos and bars next time.] 
CHECK (foo <= Bar)

Thanks in advance!

@KiddoV KiddoV added the type:question general questions label Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants