user2943416
user2943416

Reputation: 1

Error handling for SQL UNIQUE Constraint?

Can there be any proper error handling be implemented into a SQL UNIQUE Constraint? So far i'm obviously getting: Error: Duplicate entry 'test1' for key 'username'

But i'd like to have it display a custom comment if possible

Upvotes: 0

Views: 1211

Answers (1)

peter.petrov
peter.petrov

Reputation: 39457

You should check in your app code before inserting this
row that no other row exists with the same username.

Then if it exists, show the error you want to the user.

Parsing this unique constraint error
(after you get it) is not a good idea.

So do it before, not after.

Upvotes: 2

Related Questions