Skip to content

Commit

Permalink
dialect/sql/sqlgraph: support nil errors in constraint checks (#3701)
Browse files Browse the repository at this point in the history
* added nil checks to constraint errors

* removed whitespace from nil error checks

---------

Co-authored-by: Luc van Kessel <[email protected]>
  • Loading branch information
lucvankessel and Luc van Kessel authored Aug 18, 2023
1 parent dc8ea50 commit d877463
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dialect/sql/sqlgraph/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func IsConstraintError(err error) bool {
// IsUniqueConstraintError reports if the error resulted from a DB uniqueness constraint violation.
// e.g. duplicate value in unique index.
func IsUniqueConstraintError(err error) bool {
if err == nil {
return false
}
for _, s := range []string{
"Error 1062", // MySQL
"violates unique constraint", // Postgres
Expand All @@ -33,6 +36,9 @@ func IsUniqueConstraintError(err error) bool {
// IsForeignKeyConstraintError reports if the error resulted from a database foreign-key constraint violation.
// e.g. parent row does not exist.
func IsForeignKeyConstraintError(err error) bool {
if err == nil {
return false
}
for _, s := range []string{
"Error 1451", // MySQL (Cannot delete or update a parent row).
"Error 1452", // MySQL (Cannot add or update a child row).
Expand Down

0 comments on commit d877463

Please sign in to comment.