MCPcopy
hub / github.com/ent/ent / IsForeignKeyConstraintError

Function IsForeignKeyConstraintError

dialect/sql/sqlgraph/errors.go:41–56  ·  view source on GitHub ↗

IsForeignKeyConstraintError reports if the error resulted from a database foreign-key constraint violation. e.g. parent row does not exist.

(err error)

Source from the content-addressed store, hash-verified

39// IsForeignKeyConstraintError reports if the error resulted from a database foreign-key constraint violation.
40// e.g. parent row does not exist.
41func 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.

Callers 3

ConstraintChecksFunction · 0.92
TestIsConstraintErrorFunction · 0.85
IsConstraintErrorFunction · 0.85

Calls 2

ErrorMethod · 0.65
ContainsMethod · 0.45

Tested by 2

ConstraintChecksFunction · 0.74
TestIsConstraintErrorFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…