Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Proposal to have sqlc generate string constants for constraints #3763

Unanswered
Discussion options

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.

You must be logged in to vote

Replies: 1 comment

Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /