Though I can check with the postgres returned error code but I don't want not to depend on postgres database and would like to handle with gorm. Recently, the support of ErrDuplicatedKey added that really helped to check the duplication using gorm.
I checked the following errors but nothing seems to check foreign key violates error. Am I missing something?
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: amirejaz75
PRs added for the fixation of this issue: - https://github.com/go-gorm/postgres/pull/185 - https://github.com/go-gorm/gorm/pull/6329 @jinzhu please have a look at these PRs.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: markhildreth-gravity
Before https://github.com/go-gorm/postgres/pull/185/files, when a constraint failed, we were able to detect the constraint name by using the underlying pgconn.PgError type.
user, err := query.Create(&user)
if err != nil {
var pgError *pgconn.PgError
if errors.As(err, &pgError) {
if pgError.Code == UniqueViolation && pgError.ConstraintName == "users_email_key" {
// Handle specifically the email constraint broken
}
// Handle unknown error
}
}
However, this is no longer possible, as now when a unique constraint violation occurs, gorm postgres discards the underlying error and only returns the gorm-specificgorm.ErrForeignKeyViolated value, which does not include the constraint name.
Is there a different way to get access to the name of the constraint that was violated? Or a way that the original error could be included in the resulting gorm error?
Comment From: saeidee
This feature will be enabled/disabled by TranslateError flag so you may keep it disabled to have the driver error. https://github.com/go-gorm/gorm/blob/master/gorm.go#L51
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days