Ruby on Rails -sqlite3 :: CondraintaintException:不是无效约束失败

发布于 2025-02-07 11:50:17 字数 992 浏览 0 评论 0原文

我对我的项目有疑问。我正在创建一个有关燃料价格的小型项目,用户进入当前的燃料价格。要输入价格,请转到: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

这是我的存储库:

https://github.com/filipk00/p-aplikacje-mobilne

这是我的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:

https://github.com/FilipK00/p-aplikacje-mobilne

Here is my ERD schema:
erd_schema

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2025-02-14 11:50:17

param PETROL_STATION_ID正在超出哈希的范围 post params:

Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"pb_price"=>"1", "diesel_price"=>"1"}, "commit"=>"Create Post", "petrol_station_id"=>"1", "post_date_id"=>"1"}

这是造成无效错误的原因,它试图在petrol_station_id <petrol_station_id < /代码>帖子。

由于您在创建操作的开头中搜索petrolstation

@petrol_station = PetrolStation.find(params[:petrol_station_id])

在下面添加代码@post = @post_date.posts.new(post_params)应该做工作:

@post.petrol_station_id = @petrol_station.id

The param petrol_station_id is going outside the hash for post params:

Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"pb_price"=>"1", "diesel_price"=>"1"}, "commit"=>"Create Post", "petrol_station_id"=>"1", "post_date_id"=>"1"}

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:

@petrol_station = PetrolStation.find(params[:petrol_station_id])

Adding the code below @post = @post_date.posts.new(post_params) should do the job:

@post.petrol_station_id = @petrol_station.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文