Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

ruby - Rails null value in column error (only in test)

I created a new model and associated migration but I haven't created any instances of this model. My development server runs fine but when I run my tests every test fails with the following error:

DRb::DRbRemoteError: PG::NotNullViolation: ERROR: null value in column "some_value" of relation "table_name" violates not-null constraint

some_value is a non-null column but I haven't set a default because there isn't a sensible default and I want the field to be properly provided any time a record is created. Given that I haven't created any records and I am not running any test code using this model yet, what is causing the issue?

question from:https://stackoverflow.com/questions/65855787/rails-null-value-in-column-error-only-in-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I hadn't manually created any instances of this model, but as usual, Rails creates fixtures for each new model including some dummy data. In this case, the non-null column was provided a value (Rails defaults to MyString), however this field is an enum, so the string doesn't store to the database (where an enum is represented as an integer) so it comes through as null. The issue only occurs in test (and occurs in every test) because fixtures are loaded for each test into the database).

The solution is simple: Provide a valid enum value or remove unneeded fixtures!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...