rails 列不能为空:

发布于 2024-09-28 01:47:33 字数 1730 浏览 2 评论 0原文

我注意到我的应用程序会自动将所有流程设置为在创建后立即完成。我浏览了一下,找不到为什么在没有被告知的情况下更新日期,但我发现我的观点之一是寻找一个不存在的字段。我创建了“完整”字段,将其设置为非空布尔值,然后再次尝试。

我再次将其自动设置为 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 技术交流群。

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

发布评论

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

评论(2

爱冒险 2024-10-05 01:47:33

您可能通过迁移中的某些内容强制执行了数据库约束,即该列不能包含 NULL 值:

add_column :decommissions, :complete, :boolean, :null => false

您可能还希望该列的默认值为 false 而不是 NULL

我个人而言,每当我向其中添加布尔列时,我都会养成这样的习惯:始终设置 :default => false (或 true),这有助于避免在检查范围等中的 true 或 false 时出现逻辑错误,忘记如果设置不正确,该值有时可能为 null。

您可以从 mysql 提示符确认这是问题:

mysql> show indexes from decommissions;

应该有一个 Non_unique 设置为 0 且 Column_name 为完整的索引。

要解决此问题,您可以添加执行以下操作的迁移:

change_column_default(:decommissions, :complete, false)

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 than NULL

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:

mysql> show indexes from decommissions;

there should be an index with Non_unique set to 0 and Column_name of complete.

To fix this you can add a migration performing:

change_column_default(:decommissions, :complete, false)
轻许诺言 2024-10-05 01:47:33

@zetetic

我确实有一个名为complete的方法,我将其重命名,但问题仍然发生。

以下所有值均出现问题:
“处置”=>“处置”,
“完整”=>“假”,
"存储"=>"本地",
"contract_maintenance"=>"0"

这些值是使用普通形式选择器而不是轨道选择器接收的。这是我能发现他们唯一的共同点。我最终运行了一些实验测试,发现这些值没有存储在退役对象中,而是存储在它们自己的中。

因此,在控制器中,我运行了几行额外的行来将停用对象中的值设置为等于与其关联的参数:

@decommission.storage = params[:storage]
@decommission.contract_maintenance = params[:contract_maintenance]
@decommission.disposition = params[:disposition]
@decommission.complete = false

@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:

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