IsForeignKeyConstraintError reports if the error resulted from a database foreign-key constraint violation. e.g. parent row does not exist.
(err error)
| 39 | // IsForeignKeyConstraintError reports if the error resulted from a database foreign-key constraint violation. |
| 40 | // e.g. parent row does not exist. |
| 41 | func IsForeignKeyConstraintError(err error) bool { |
| 42 | if err == nil { |
| 43 | return false |
| 44 | } |
| 45 | for _, s := range []string{ |
| 46 | "Error 1451", // MySQL (Cannot delete or update a parent row). |
| 47 | "Error 1452", // MySQL (Cannot add or update a child row). |
| 48 | "violates foreign key constraint", // Postgres |
| 49 | "FOREIGN KEY constraint failed", // SQLite |
| 50 | } { |
| 51 | if strings.Contains(err.Error(), s) { |
| 52 | return true |
| 53 | } |
| 54 | } |
| 55 | return false |
| 56 | } |
| 57 | |
| 58 | // IsCheckConstraintError reports if the error resulted from a database check constraint violation. |
| 59 | // e.g. a value does not satisfy a check condition. |
searching dependent graphs…