Ruby on Rails -sqlite3 :: CondraintaintException:不是无效约束失败
我对我的项目有疑问。我正在创建一个有关燃料价格的小型项目,用户进入当前的燃料价格。要输入价格,请转到:PETROL_STATION-> post_date->邮政。在帖子中,有两个输入字段:diesel_price和pb_price。这是我在数据库中的帖子。但是,当我尝试添加价格(帖子)时,我会有这个错误。
我尝试从DB/模式中删除“非零”。我也尝试更改ERD模式,但我认为我已经得到了所有内容。
这是我的错误:
ActiveRecord::NotNullViolation in PostsController#create
SQLite3::ConstraintException: NOT NULL constraint failed: posts.petrol_station_id
Extracted source (around line #32):
respond_to do |format|
if @post.save
format.html { redirect_to [@petrol_station, @post_date], notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
这是我的存储库:
这是我的ERD模式: erd_schema
I have question about my project. I am creating a small project on fuel prices, where users enter the current price of fuel. To enter the price, go to: petrol_station -> post_date -> post. In post, there are two input fields: diesel_price and pb_price. This is my post in database. But I have this error when I try to add price (post).
I've tried deleted "NOT NULL" from db/schema. I tried also changing my ERD schema, but I think I have got everything what is obligatory.
Here is my ERROR:
ActiveRecord::NotNullViolation in PostsController#create
SQLite3::ConstraintException: NOT NULL constraint failed: posts.petrol_station_id
Extracted source (around line #32):
respond_to do |format|
if @post.save
format.html { redirect_to [@petrol_station, @post_date], notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
Here is my repository:
Here is my ERD schema:
erd_schema
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
param
PETROL_STATION_ID
正在超出哈希的范围 post params:这是造成无效错误的原因,它试图在
petrol_station_id <
petrol_station_id < /代码>帖子。
由于您在创建操作的开头中搜索
petrolstation
:在下面添加代码
@post = @post_date.posts.new(post_params)
应该做工作:The param
petrol_station_id
is going outside the hash for post params:This is the reason for the NOT NULL error, it tries to save a nil value in the
petrol_station_id
for the Post.Since you search for the
PetrolStation
in the beginning of the create action:Adding the code below
@post = @post_date.posts.new(post_params)
should do the job: