rails 列不能为空:
我注意到我的应用程序会自动将所有流程设置为在创建后立即完成。我浏览了一下,找不到为什么在没有被告知的情况下更新日期,但我发现我的观点之一是寻找一个不存在的字段。我创建了“完整”字段,将其设置为非空布尔值,然后再次尝试。
我再次将其自动设置为 true,因此我尝试在 create 方法中将其显式设置为 false,但这仍然不起作用:S 所以我尝试将隐藏字段放入表单中。即使正在提供该值,我现在仍收到“列不能为空”错误。
正如您所看到的,显然有一个完整值的参数。我缺少什么?
错误:
Mysql::Error: Column 'complete' cannot be null: INSERT INTO `decommissions` (`completed_at`, `keep_backups`, `services_stopped`, `updated_at`, `operating_system_id`, `comments`, `username`, `disposition`, `stakeholder_email`, `complete`, `alias`, `storage`, `model_id`, `contract_maintenance`, `created_at`) VALUES(NULL, 1, 1, '2010-10-18 00:32:37', 1, NULL, NULL, '', '[email protected]', NULL, 'test1', '', 1, '', '2010-10-18 00:32:37')
参数:
{"decommission"=>{"dns_items_attributes"=>{"0"=>{"ip"=>"131.181.185.111",
"alias"=>"test",
"retain"=>"1",
"_destroy"=>""}},
"keep_backups"=>"1",
"services_stopped"=>"1",
"operating_system_id"=>"1",
"stakeholder_email"=>"[email protected]",
"alias"=>"test1",
"model_id"=>"1"},
"commit"=>"Submit",
"authenticity_token"=>"cMMf0zS/5jPExlXqVPaYVXndqPeVkm+OQ/WEPIYd2+g=",
"disposition"=>"Dispose",
"complete"=>"false",
"storage"=>"Local",
"contract_maintenance"=>"0"}
当我将以下内容放入创建控制器时,它呈现“true”:
@decommission = Decommission.new(params[:decommission])
@decommission.complete = false
render :text => @decommission.complete
I noticed that my application was automatically setting all my processes to having been completed as soon as they were created. I looked through it, couldn't find why the date was being updated without being told to, but I found that one of my views was looking for a field that didn't exist. I created the 'complete' field, set it as a not null boolean and tried again.
I was getting it being set automatically to true again so I tried setting it as false explicitly in the create method, this still didn't work :S so I tried putting a hidden field into the form. I am now getting a "column cannot be null" error even though the value is being provided.
As you can see there is clearly a parameter for the complete value. What am I missing?
Error:
Mysql::Error: Column 'complete' cannot be null: INSERT INTO `decommissions` (`completed_at`, `keep_backups`, `services_stopped`, `updated_at`, `operating_system_id`, `comments`, `username`, `disposition`, `stakeholder_email`, `complete`, `alias`, `storage`, `model_id`, `contract_maintenance`, `created_at`) VALUES(NULL, 1, 1, '2010-10-18 00:32:37', 1, NULL, NULL, '', '[email protected]', NULL, 'test1', '', 1, '', '2010-10-18 00:32:37')
Parameters:
{"decommission"=>{"dns_items_attributes"=>{"0"=>{"ip"=>"131.181.185.111",
"alias"=>"test",
"retain"=>"1",
"_destroy"=>""}},
"keep_backups"=>"1",
"services_stopped"=>"1",
"operating_system_id"=>"1",
"stakeholder_email"=>"[email protected]",
"alias"=>"test1",
"model_id"=>"1"},
"commit"=>"Submit",
"authenticity_token"=>"cMMf0zS/5jPExlXqVPaYVXndqPeVkm+OQ/WEPIYd2+g=",
"disposition"=>"Dispose",
"complete"=>"false",
"storage"=>"Local",
"contract_maintenance"=>"0"}
When I put the following in my create controller it renders 'true':
@decommission = Decommission.new(params[:decommission])
@decommission.complete = false
render :text => @decommission.complete
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能通过迁移中的某些内容强制执行了数据库约束,即该列不能包含
NULL
值:add_column :decommissions, :complete, :boolean, :null => false
您可能还希望该列的默认值为
false
而不是NULL
就我个人而言,每当我向其中添加布尔列时,我都会养成这样的习惯:始终设置
:default => false
(或 true),这有助于避免在检查范围等中的 true 或 false 时出现逻辑错误,忘记如果设置不正确,该值有时可能为 null。您可以从 mysql 提示符确认这是问题:
应该有一个
Non_unique
设置为 0 且Column_name
为完整的索引。要解决此问题,您可以添加执行以下操作的迁移:
You've probably enforced a database constraint that the column cannot contain a
NULL
value via something in a migration like:add_column :decommissions, :complete, :boolean, :null => false
You'll probably also want the default value of the column to be
false
rather thanNULL
Personally I make a habit of whenever I add a boolean column to always set
:default => false
(or true), which helps to avoid logic bugs where you are checking for true or false in scopes etc, forgetting that the value can sometimes be null, if you don't set it right.You can confirm is this is the issue from a mysql prompt:
there should be an index with
Non_unique
set to 0 andColumn_name
of complete.To fix this you can add a migration performing:
@zetetic
我确实有一个名为complete的方法,我将其重命名,但问题仍然发生。
以下所有值均出现问题:
“处置”=>“处置”,
“完整”=>“假”,
"存储"=>"本地",
"contract_maintenance"=>"0"
这些值是使用普通形式选择器而不是轨道选择器接收的。这是我能发现他们唯一的共同点。我最终运行了一些实验测试,发现这些值没有存储在退役对象中,而是存储在它们自己的中。
因此,在控制器中,我运行了几行额外的行来将停用对象中的值设置为等于与其关联的参数:
@zetetic
I did have a method called complete, I renamed this but the problem was still occurring.
The problem occurred for all the following values:
"disposition"=>"Dispose",
"complete"=>"false",
"storage"=>"Local",
"contract_maintenance"=>"0"
These values were received using normal form selectors rather than rails ones. That was the only thing I could find that they had in common. I ended up running some experimental tests and found that the values were not being stored withing the decommission object, but rather in their own.
So in the controller I ran a few extra lines to set the values within the decommission object to equal the parameters associated with it: