Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@rotemtam
Copy link
Contributor

@rotemtam rotemtam commented Mar 9, 2021

…constraint error and if it is related to uniqueness or FK

Copy link
Member

@a8m a8m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @rotemtam. Thanks for adding this. I've added minor suggestions.

Please use the following commit format when it's possible, it makes my life easier when I'm releasing a new version.

<package>: <message>

For example

dialect/sql/sqlgraph: add constraint error checks

Fixed #ISSUE-NUMBER

Comment on lines 19 to 21
"mysql": "Error 1062",
"postgres": "violates unique constraint",
"sqlite": "UNIQUE constraint failed",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use dialect.MySQL, dialect.PostgreSQL, etc instead of these strings. Why do we need this, and not just [3]string{...}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't, i'm removing

}

// IsUniquenessConstraintError returns true if the error resulted from a DB uniqueness constraint error
func IsUniquenessConstraintError(err error) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func IsUniquenessConstraintError(err error) bool {
func IsUniqueConstraintError(err error) bool {

Uniqueness is too long. Let's change it to Unique.

Comment on lines 11 to 16
// IsConstraintError returns true if the error resulted from a DB constraint error
func IsConstraintError(err error) bool {
return IsUniquenessConstraintError(err) || IsForeignKeyConstraintError(err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// IsConstraintError returns true if the error resulted from a DB constraint error
func IsConstraintError(err error) bool {
return IsUniquenessConstraintError(err) || IsForeignKeyConstraintError(err)
}
// IsConstraintError returns true if the error resulted from a DB constraint error.
func IsConstraintError(err error) bool {
var e *ConstraintError
return errors.As(err, &e) || IsUniquenessConstraintError(err) || IsForeignKeyConstraintError(err)
}

It does make sense to return true is the error is type ConstraintError as well. Also, please add more details regarding the possible constraint errors.

return IsUniquenessConstraintError(err) || IsForeignKeyConstraintError(err)
}

// IsUniquenessConstraintError returns true if the error resulted from a DB uniqueness constraint error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// IsUniquenessConstraintError returns true if the error resulted from a DB uniqueness constraint error
// IsUniqueConstraintError reports if the error resulted from a DB uniqueness constraint violation. e.g. duplicate value in unique index.

return false
}

// IsForeignKeyConstraintError returns true if the error resulted from a DB FK constraint error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// IsForeignKeyConstraintError returns true if the error resulted from a DB FK constraint error
// IsForeignKeyConstraintError reports if the error resulted from a DB FK constraint violation. e.g. parent row does not exist.

@rotemtam rotemtam force-pushed the feat/constraint-errors-sqlgraph branch from ad85e8c to 282d7bb Compare March 9, 2021 12:38
require.False(t, pets[2].QueryOwner().ExistX(ctx))
}

func ConstraintChecks(t *testing.T, client *ent.Client) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove them after it's added to templates (it should be in sqlgraph).

@a8m a8m merged commit 2a17dba into ent:master Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants