Tests not running, fixture fails to insert into database, no indication of which fixture

I’m using Rails 6.0.3.1

I’m trying to run a controller test, and it gives me this output:

DRb::DRbRemoteError: PG::NotNullViolation: ERROR:  null value in column "customer_id" violates not-null constraint
DETAIL:  Failing row contains (980190962, 980190962, 1, null, 980190962, 2022-08-03 12:06:04, 2022-08-03 12:06:04, null, null, null, null, null, 2022-08-16 21:35:16.252522, 2022-08-16 21:35:16.252522)

I can’t tell what fixture Rails is trying to create from this output, and I have many.

Is there some way to narrow this down?

Thanks.

I get an error message similar to this: PG::NotNullViolation: ERROR: null value in column "customer_id" of relation "orders" violates not-null constraint - note the extra of relation "orders". So you can just scan the orders.yml file and find where customer_id is blank.

Can you reproduce your error on the rails’ main branch? If so, can you provide a reproducible test case using one of the templates - rails/guides/bug_report_templates at main · rails/rails · GitHub or create a sample app and provide a link to it?

Free advice: the backtrace is a very valuable resource for debugging any issue.

Since I have no backtrace, I can only do a guesswork, and based on that, may suggest the following: have a closer look at the source code of ActiveRecord::FixtureSet.read_and_insert and ActiveRecord::FixtureSet.insert methods. In first one, fixture_files are “converted” into instances of ActiveRecord::FixtureSet; in second they are inserted into a database. I would suggest adding a debugger somewhere around there, or even something as simple as

puts "Inserting", set.instance_variable_get(:@path).inspect

between lines 629 and 630, that should print which file is about to be inserted. The last printed file before exception should be your culprit. I may be wrong, though, and further poking around may be necessary.

You can do the debugging by either editing the gem files directly (faster, but don’t forget to revert your changes or reinstall the gem), or by monkeypatching those methods or even the whole class within your project only (safer).

1 Like

Great suggestions, thanks!