gorm: POSTGRES How to check if error type is DUPLICATE KEY
OLD Ticket: https://github.com/go-gorm/gorm/issues/4037
I did not find any solution for postgres yet. I searched everywhere but still there is no way i found to check the duplicate key for postgres. @jinzhu Can you provide the PSQL example like you did for the Mysql in old ticket. Thanks
Your Question
I have unique index key and when i try to insert duplicate record i get this error ERROR: duplicate key value violates unique constraint "idx_itemid_partid" (SQLSTATE 23505) but i want to show different message to the frontend based on ERROR TYPE but in gorm i do not see any Duplicate error type please check screenshot. How do i use the validation for duplicate error type
if err != nil && errors.Is(err, gorm.ErrDuplicateKey) { // how do i do this ?
fmt.Println("Duplicate key found.")
}
The document you expected this should be explained
N/A
Expected answer
Should have error type duplicate
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 22
@spiside
It gives the error
But anyway i figured it out i used the fmt.Println(reflect.TypeOf(err)) and it returns
*pgconn.PgErrorit seems goorm uses pgconn package to connect to the PostgresThen i used below code and it worked šÆ
This stackoverflow answer should provide some details on how errors are commonly caught using Postgres error codes. For your problem specifically, I think you can do the following:
Hereās a list of all the SQLSTATE error codes that are used in Postgres. Youāll see that
23505is unique violation error.Iāve solved my previous problem. I get this to work by installing
github.com/jackc/pgxinstead ofgithub.com/jackc/pgconn. Iām now using*pgconn.PgErrorfromgithub.com/jackc/pgx/v5/pgconnimport. See the image below. Hope it helps for anyone that got the same problem with mine.Thanks so much for your comment!! It helped me a lot !!
@khanakia Iād agree if we didnāt have the struct tags for unique indexes. seems weird to support writing it but not checking violations
@khanakia referencing your image below. For example, I want to keep my gorm db implementation separated from Postgres (flexibility to change DBs). However, when handling unique key errors, itās impossible to know itās a unique key error without knowing itās a Postgres unique key error.
I was thinking of a general āgorm.ErrUniqueKeyViolationā like you referenced in your question. If itās not a good idea, just curious why not.
I was hoping
Is()might do the heavy lifting here but this is how Iāve defined a function that checks postgres error codes:IsErrorCode()casts the error to apq.Error, and if it is, it will check the error code to see if it matches.This problem occurs with āgorm.io/driver/postgres v1.5.0ā (gorm.io/gorm v1.25.1). To make the type assertion work as expected, downgrade the version of gorm.io/driver/postgres to 1.4.5.
I checked the error codes with pq library
err :=db.Create(&user).Error
if err !=nil {
if err.(*pq.Error).Code == ā23505ā { fmt.Println(āUsername already existsā)
}
}
Here is how I use
Example of usage:
This worked for me
23505 code represent duplicate entry error
@spiside Thank for your great support along the way. I really appreciate it by heart. š
@spiside
Still returning false
FYI: I did unmarshall the error OBJECT to JSON this is what i see but still none of the above code works