Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k 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?


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

1 Reply

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...