Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.5k views
Welcome To Ask or Share your Answers For Others

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share

548k questions

547k answers

4 comments

56.5k users

...