Skip to content

feat: Unique Constraint Violation error translator for different drivers #6004

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

Merged
merged 5 commits into from
Mar 6, 2023

Conversation

saeidee
Copy link
Member

@saeidee saeidee commented Jan 29, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

It will enable developers to have a better error response when there is a unique key constraint violation detected in different SQL engines.

Addresses these issues: #4037, #4135, #5651, #6002.

There is no new import or any dependency on any driver, the implementation works by marshaling the returned error from any query and comparing it to SQL engine's default error message by looking after the error code.

err = DB.Create(&City{Name: "Madrid"}).Error
if errors.Is(err, gorm.ErrDuplicatedKey) {
    // some logic here
}

User Case Description

When there is a unique key constraint violation error it will return gorm.ErrDuplicatedKey. Basically, it will look if there is a translation available for a specific type of error, if there was one it will return that translation, otherwise, it will return the given untranslated error.

@saeidee saeidee changed the title feat: duplicated key error translator for different drivers feat: Unique Constraint Violation error translator for different drivers Jan 29, 2023
@a631807682
Copy link
Member

That's nice, but we'd prefer it to be a plugin , or implemented per drivers rather than in gorm.

@jinzhu
Copy link
Member

jinzhu commented Feb 18, 2023

Maybe we can add TranslateError support to dialector.

If the dialector implements TranslateError support, will use it to translate the error when AddError for db.

@saeidee
Copy link
Member Author

saeidee commented Feb 19, 2023

Maybe we can add TranslateError support to dialector.

If the dialector implements TranslateError support, will use it to translate the error when AddError for db.

This is a good idea, I will update the PR & also create a new PR for one of the drivers.

@saeidee saeidee force-pushed the saeidee_support-for-duplicated-key branch from c36db2a to d975b80 Compare February 21, 2023 16:23
@jinzhu jinzhu merged commit 85eaf9e into go-gorm:master Mar 6, 2023
@saeidee saeidee deleted the saeidee_support-for-duplicated-key branch March 6, 2023 19:49
@ZhouXing19
Copy link

ZhouXing19 commented Mar 15, 2023

Hello, will this new ErrDuplicatedKey appear in the next patch (i.e. 1.24.7)? Thanks!

@guillermo-st
Copy link

Hello, will this new ErrDuplicatedKey appear in the next patch (i.e. 1.24.7)? Thanks!

I would like to know too! And while I'm at it, how should I go about updating my project dependencies to use the new gorm release that has this error included?

@saeidee
Copy link
Member Author

saeidee commented Apr 10, 2023

Hello, will this new ErrDuplicatedKey appear in the next patch (i.e. 1.24.7)? Thanks!

I would like to know too! And while I'm at it, how should I go about updating my project dependencies to use the new gorm release that has this error included?

Documentation has been added go-gorm/gorm.io#664

bfabio added a commit to italia/developers-italia-api that referenced this pull request Jun 1, 2023
Remove common.ErrDBUniqueConstraint, which was used as a workaround
for the lack of abstraction in gorm.

With the update in go-gorm/gorm#6004 we can now
fully use gorm for db related errors (fe. gorm.ErrDuplicatedKey).
bfabio added a commit to italia/developers-italia-api that referenced this pull request Jun 1, 2023
Remove common.ErrDBUniqueConstraint, which was used as a workaround
for the lack of abstraction in gorm.

With the update in go-gorm/gorm#6004 we can now
fully use gorm for db related errors (fe. gorm.ErrDuplicatedKey).
bfabio added a commit to bfabio/developers-italia-api that referenced this pull request Jun 16, 2023
Remove common.ErrDBUniqueConstraint, which was used as a workaround
for the lack of abstraction in gorm.

With the update in go-gorm/gorm#6004 we can now
fully use gorm for db related errors (fe. gorm.ErrDuplicatedKey).
bfabio added a commit to bfabio/developers-italia-api that referenced this pull request Jun 16, 2023
Remove common.ErrDBUniqueConstraint, which was used as a workaround
for the lack of abstraction in gorm.

With the update in go-gorm/gorm#6004 we can now
fully use gorm for db related errors (fe. gorm.ErrDuplicatedKey).
bfabio added a commit to italia/developers-italia-api that referenced this pull request Jun 17, 2023
Remove common.ErrDBUniqueConstraint, which was used as a workaround
for the lack of abstraction in gorm.

With the update in go-gorm/gorm#6004 we can now
fully use gorm for db related errors (fe. gorm.ErrDuplicatedKey).
bfabio added a commit to italia/developers-italia-api that referenced this pull request Jun 17, 2023
Remove common.ErrDBUniqueConstraint, which was used as a workaround
for the lack of abstraction in gorm.

With the update in go-gorm/gorm#6004 we can now
fully use gorm for db related errors (fe. gorm.ErrDuplicatedKey).
bfabio added a commit to italia/developers-italia-api that referenced this pull request Jun 17, 2023
Remove common.ErrDBUniqueConstraint, which was used as a workaround
for the lack of abstraction in gorm.

With the update in go-gorm/gorm#6004 we can now
fully use gorm for db related errors (fe. gorm.ErrDuplicatedKey).
bfabio added a commit to italia/developers-italia-api that referenced this pull request Jun 17, 2023
Remove common.ErrDBUniqueConstraint, which was used as a workaround
for the lack of abstraction in gorm.

With the update in go-gorm/gorm#6004 we can now
fully use gorm for db related errors (fe. gorm.ErrDuplicatedKey).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants