Replies: 4 comments 9 replies
-
This would be nice. The primary bottleneck will be reading the constraint information from each DB. Once that is done, generating code would be relatively straightforward. |
Beta Was this translation helpful? Give feedback.
-
@mbezhanov I saw you have some plans that (I think) are going in this direction (#237 (comment)) What are your thoughts on this? |
Beta Was this translation helpful? Give feedback.
-
@daddz I'll look into this in the following days and make a proposal. |
Beta Was this translation helpful? Give feedback.
-
@daddz @stephenafamo so I put some thought into this, and here's what I came up with. As you know, the if _, err := models.Pilots.Insert(...); err != nil {
if errors.Is(models.PilotErrors.ErrUniqueName, err) {
// do something to handle the error
}
} To facilitate this, we could have the following type definition in type errorUniqueConstraint struct {
name string // the name of the unique constraint
}
func (e *errorUniqueConstraint) Error() string {
return e.name
}
func (e *errorUniqueConstraint) Is(target error) bool {
err, ok := target.(*mysql.MySQLError)
if !ok {
return false
}
return err.Number == 1062 && strings.Contains(err.Message, e.name)
} Then, in the generated type pilotErrors struct {
ErrUniqueName error
}
var PilotErrors = pilotErrors{
ErrUniqueName: &errorUniqueConstraint{name: "pilots_name_IDX"},
} Obviously some of the hard-coded things (e.g. the constraint name, the MySQL error code, etc.) can be defined as constants and variables. What do you think? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I saw this nice PR: #237 and it sparked an idea but I don't know if it's feasible.
Would it be possible to also generate variables/fields for the (named?) constraints on a table?
Bogus example:
Would generate some fields like this:
Which could be checked against in error cases:
Maybe it could also be used in e.g. conflicts for
Upsert
for constraints that span multiple columns:I think something like this could greatly enhance error and conflict handling.
I didn't look into the PR in detail yet so the generation part might be solved/available already
Beta Was this translation helpful? Give feedback.
All reactions