mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen/template/dialect/sql: cleaning up isSQLConstraintError (#1319)
* entc/gen/template/dialect/sql: cleaning up isSQLConstraintError in generated ent.go to use new sqlgraph IsConstraintError checks. Adding IsFK and IsUniqueness methods to the generated ent.ConstraintError (on SQL dialects) struct to allow users to glean more information about the specific constraint violation. Fixes #1310 * fix indentation in tmpl file regen files rm new methods from generated code * regen
This commit is contained in:
@@ -9,7 +9,6 @@ package ent
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
@@ -242,22 +241,8 @@ func IsConstraintError(err error) bool {
|
||||
}
|
||||
|
||||
func isSQLConstraintError(err error) (*ConstraintError, bool) {
|
||||
var (
|
||||
msg = err.Error()
|
||||
// error format per dialect.
|
||||
errors = [...]string{
|
||||
"Error 1062", // MySQL 1062 error (ER_DUP_ENTRY).
|
||||
"UNIQUE constraint failed", // SQLite.
|
||||
"duplicate key value violates unique constraint", // PostgreSQL.
|
||||
}
|
||||
)
|
||||
if _, ok := err.(*sqlgraph.ConstraintError); ok {
|
||||
return &ConstraintError{msg, err}, true
|
||||
}
|
||||
for i := range errors {
|
||||
if strings.Contains(msg, errors[i]) {
|
||||
return &ConstraintError{msg, err}, true
|
||||
}
|
||||
if sqlgraph.IsConstraintError(err) {
|
||||
return &ConstraintError{err.Error(), err}, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user