sqlboiler icon indicating copy to clipboard operation
sqlboiler copied to clipboard

No insert error on foreign key exception + QueryRowContext

Open DGollings opened this issue 5 years ago • 2 comments

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

What version of SQLBoiler are you using (sqlboiler --version)?

4.2.0 Possibly relevant, using pgx driver instead of pq

What is your database and version (eg. Postgresql 10)

Cockroach 20.1.1

Further information. What did you do, what did you expect?

Inserting new row, with a nullable UUID (string) missing and using boil.Infer() causes it to append returning "stuff" to the end of the query This causes it to run QueryRowContext, but a foreign key exception is swallowed which results in the worst possible outcome: no row is inserted and no error is reported It seems this is expected? https://github.com/lib/pq/issues/77

If I boil.Greylist the missing column instead, it hits exec.ExecContext and returns the expected foreign key error

Interestingly, it does error as expected if a non nullable UUID (string) is missing. So it appears only a particular class of errors is ignored.

In short, if a foreign key error is expected:

	if len(cache.retMapping) != 0 {
= no error AND no insertion
		err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
	} else {
= error (and no insertion of course)
		_, err = exec.ExecContext(ctx, cache.query, vals...)
	}

DGollings avatar Jul 07 '20 12:07 DGollings

Sorry, I'm trying to understand where the error is getting swallowed.

The template looks like this and is obviously returning the error if there is one.

The issue you linked to is closed and has a related and also closed/fixed Go issue: https://github.com/golang/go/issues/6651 so I'd be confused why this is still an issue.

Have you tried with the pg driver to see if you get the same behavior? That should be pretty trivial to try.

aarondl avatar Jul 19 '20 23:07 aarondl

This is where the error is being swallowed: err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) and it appears that only happens with foreign key constraint errors.

Changing it to:

_, err = exec.ExecContext(ctx, cache.query, vals...)
err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)

ie: almost the same statement (but not quite), makes the foreign key constraint error show up on the first line. It's how I figured this out in the first place.

I'll have to try the pg driver, thanks

DGollings avatar Jul 20 '20 08:07 DGollings