Ruby on rails 遇到PG::NotNull冲突
好的,所以我把money rails gem集成到我的基本计算应用程序中 当我现在尝试运行它时,出现以下错误: ActiveRecord::语句在TippiesControllercreate中无效 下面这条线: PG::NotNullViolation:错误:列提示中的null值违反了非null约束详细信息:失败的行包含37,null,null,2017-03-10 07:47:36.152504,2017-03-10 07:47:36.152504,3300,USD,10,USD.:插入到创建时间、更新时间、成本、小费价值$1、$2、$3、$4的小费中返回id 所以,我认为这意味着它产生了这个,因为我的模型规定了验证:提示,状态:真实。。。所以我把它注释掉了,但仍然收到这个错误 我不知道该怎么解决。如果您能帮我弄清楚,我们将不胜感激 当前模型 Show.html.erb此页面在主页后弹出,这是新操作Ruby on rails 遇到PG::NotNull冲突,ruby-on-rails,ruby,money-rails,Ruby On Rails,Ruby,Money Rails,好的,所以我把money rails gem集成到我的基本计算应用程序中 当我现在尝试运行它时,出现以下错误: ActiveRecord::语句在TippiesControllercreate中无效 下面这条线: PG::NotNullViolation:错误:列提示中的null值违反了非null约束详细信息:失败的行包含37,null,null,2017-03-10 07:47:36.152504,2017-03-10 07:47:36.152504,3300,USD,10,USD.:插入到创
<br/><br/>
<h1 class="text-center">Your Total Cost</h1>
<br/><br />
<table class="table table-striped">
<tr>
<td>
Cost of Your Meal:
</td>
<td>
<%= humanized_money_with_symbol @tippy.cost_cents %>
</td>
</tr>
<tr>
<td>
Tip You Picked:
</td>
<td>
<%= number_to_percentage(@tippy.tip_cents * 100, format: "%n%", precision: 0) %>
</td>
</tr>
<tr>
<td>
The Total Cost:
</td>
<td>
<%= humanized_money_with_symbol @tippy.calculation_of_total_cost %>
</td>
</tr>
</table>
new.html.erb
_form.html.erb
此错误来自数据库,而不是Rails 在添加tip列的迁移中,它将如下所示:
t.float :tip, null: false
这将转换为SQL模式中的NOTNULL约束
数据库约束和验证的用途不同:前者用于明确防止不正确的数据被持久化,而后者是通过自动添加的ActiveModel::errors对象将错误反馈给用户的一种简单方法
要解决您的问题,您必须添加另一个迁移,并使tip再次为空,并可能提供适当的默认值:
change_column :tippies, :tip, :float, null: true, default: 0
那么我该如何解决这个问题呢?@user27307254534534543675765刚刚添加了一点:-这是一个有害的建议。删除数据库约束会导致将来出现不可预测的偶发问题。解决这个问题的正确方法是将tip值显式设置为0,无论它是否由用户输入提供。@MichaelKohl,谢谢您的建议。我最终做这件事是为了成本,等等。。。它现在起作用了。。。也就是说,小费一路上涨。。。这是因为我认为我不恰当地整合了money rails gem。。。你不知道这件事吗=@mudasobwa,嗯,这是一个很好的建议,将其设置为默认值。。。但是现在我还有其他问题,啊!
ActiveRecord::Schema.define(version: 20170310070749) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "tippies", force: :cascade do |t|
t.float "tip", null: false
t.decimal "cost", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "cost_cents", default: 0, null: false
t.string "cost_currency", default: "USD", null: false
t.integer "tip_cents", default: 0, null: false
t.string "tip_currency", default: "USD", null: false
end
end
<br/><br/>
<h1 class="text-center">Your Total Cost</h1>
<br/><br />
<table class="table table-striped">
<tr>
<td>
Cost of Your Meal:
</td>
<td>
<%= humanized_money_with_symbol @tippy.cost_cents %>
</td>
</tr>
<tr>
<td>
Tip You Picked:
</td>
<td>
<%= number_to_percentage(@tippy.tip_cents * 100, format: "%n%", precision: 0) %>
</td>
</tr>
<tr>
<td>
The Total Cost:
</td>
<td>
<%= humanized_money_with_symbol @tippy.calculation_of_total_cost %>
</td>
</tr>
</table>
<br /><br />
<h1 class="text-center">Calculate Your Tip!</h1>
<%= render 'form', tippy: @tippy %>
<%= form_for(tippy, :html => {'class' => "form-horizontal"}) do |f| %>
<% if tippy.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(tippy.errors.count, "error") %> prohibited this tippy from being saved:</h2>
<ul>
<% tippy.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field form-group">
<%= f.label :cost_of_your_meal, class: "control-label" %>
<%= f.text_field :cost, class: "form-control" %>
</div>
<div class="field form-group">
<%= f.label :pick_your_tip, class: "control-label" %>
<%= f.select(:tip, Tippy::TIP_CHOICES, class: "form-control") %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-primary btn-lg btn-block" %>
</div>
<% end %>
t.float :tip, null: false
change_column :tippies, :tip, :float, null: true, default: 0