Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NamedQuery does not error when foreign key integrity is violated in a CTE #880

Open
GKimbles opened this issue Jul 31, 2023 · 1 comment

Comments

@GKimbles
Copy link

It seems like NamedQueryContext and NamedQuery are not returning errors when an insert fails when using a CTE. Small example as follows

query := `
WITH inserted AS (
	INSERT INTO ticket (creator_id,  account_id, team_id) 
	VALUES (:creator_id, :account_id, :team_id)
	RETURNING id, created_at, team_id    
)
SELECT inserted.id AS id, inserted.created_at AS created_at, team.name AS team_name
FROM inserted
LEFT JOIN team ON team.id = team_id
`
resp, err := s.db.NamedQueryContext(ctx, query, mapTicketCreate(t))
if err != nil {
    return model.Ticket{}, err
}

var id string
var createdTime time.Time
var teamName string
if resp.Next() {
	err := resp.Scan(&id, &createdTime, &teamName)
	if err != nil {
		return model.Ticket{}, err
	}
}

No error is thrown in the code, but no records are inserted. Running the query directly in the DB does throw an error. createdTime and id are present in the response.

Using sqlx v1.3.5 with CockroachDB

@DaryaHom
Copy link

DaryaHom commented Jan 17, 2024

Try to check the *sqlx.Rows.Error() after scanning the response result. Like this:

if err := resp.Err(); err != nil {
	return model.Ticket{}, err
}

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

No branches or pull requests

2 participants