-
Notifications
You must be signed in to change notification settings - Fork 930
Proposal to have sqlc generate string constants for constraints #3763
-
I have some code that looks like:
obj, err = queries.SomeQuery() if err != nil { var pgErr *pgconn.PgError if errors.As(err, &pgErr) && pgErr.ConstraintName == "some_unique_key" { // Let user know about constraint violation return } // Log query failure and return 500 return }
It would be nice if I had string constants for all of my constraint names to compare against, and I think sqlc should be capable of building that for me.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
I agree this would be a great feature for sqlc to have.
I currently handle this by storing relevant constraints in a contraints.go
file under my database directory, like so:
package postgres const ( // UNIQUE KEYS UniqueUsersId = "users_id_key" UniqueGroupId = "group_id_key" UniqueUsersToGroupsUserIdGroupId = "users_to_groups_user_id_group_id_key" // FOREIGN KEYS FKeyUsersToGroupsUserId = "users_to_groups_user_id_fkey" FKeyUsersToGroupsGroupId = "users_to_groups_group_id_fkey" )
Of course, this takes manual effort and isn't foolproof. Having sqlc generate the exhaustive set of available constraints (with commented metadata) if possible would be a massive boon.
If this gets implemented, it would be a good idea to have #835 in mind, as constraints are general to a database and can be centralised in a single file (or package) rather than being repeated per-model.
Beta Was this translation helpful? Give feedback.