Rails 3:DataObjects::SQLError —— 如何阻止这些在*警告*上产生错误?

发布于 2024-11-05 02:28:34 字数 417 浏览 4 评论 0原文

当我仅将几个字段插入到旧数据库模式中的表中(通过 DataMapper)时,我得到以下信息:

DataObjects::SQLError "Field 'activationcode' doesn't have a default value"

我实际上没有为该字段指定值,但底层数据库模式 (MySQL) 没有指定值都没有一套。这实际上不会引起问题,它只是意味着 MySQL 执行插入,但在完成后显示“警告:1”。然而,这个警告正是导致 Rails 停止运行并回滚插入的原因。我知道我可以修复这个字段,但是还有数千个类似的字段,我正在寻找是否有一种方法可以让 DataObjects 冷静下来,并且只在实际 SQL 错误上出错,而​​不是在警告上出错?

Rails 3.0.7、Ruby 1.9.2、DataMapper 1.1.0

When I insert just a few fields to a table in a legacy database schema (via DataMapper), I'm getting the following:

DataObjects::SQLError "Field 'activationcode' doesn't have a default value"

I didn't actually specify a value for that field, but the underlying DB schema (MySQL) doesn't have one set neither. This doesn't actually cause an issue, it just means MySQL performs the insert but shows "Warnings: 1" after it's done. That warning is what is causing Rails to come to a grinding halt and rollback the insert, however. I know I could fix this field, but there are thousands more like it and I'm seeking to find out if there's a way to get DataObjects to just chill out and only error on actual SQL errors, not on warnings?

Rails 3.0.7, Ruby 1.9.2, DataMapper 1.1.0

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

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

发布评论

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

评论(1

过去的过去 2024-11-12 02:28:34

对于不可避免的可怜的灵魂来说,他们也必须解决这个问题...

问题是 JDBC(DataObjects 包装的)调整 MySQL 变量 sql_mode 并将其设置为 TRADITIONAL code>,将这种行为添加到 MySQL 中。您只需在使用 DataMapper 之前在应用程序中的任何位置撤消该操作(只需完成一次)。

repository(:default).adapter.execute("SET sql_mode = ''")

由于 sql_mode 可以是逗号分隔的模式列表,理想情况下,您可能想要做一些更聪明的事情,从列表中删除 TRADITIONAL ,但上面是一个快速而肮脏的解决方案。

更新:这不仅仅是导致它的传统标志。它是传统的、STRICT_ALL_TABLES 和STRICT_TRANS_TABLES。我检查了 JDBC 的配置,并通过反复试验消除了有问题的模式。不会产生 WARNING = ERROR 行为的结果列表是:

REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,NO_UNSIGNED_SUBTRACTION,NO_DIR_IN_CREATE,ANSI,NO_BACKSLASH_ESCAPES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

For the inevitable poor soul who also has to work around this issue...

The problem is that JDBC (which is what DataObjects wraps) adjusts the MySQL variable sql_mode and sets it to TRADITIONAL, which adds this behaviour to MySQL. You just need to undo that anywhere in your application before you use DataMapper (only needs to be done once).

repository(:default).adapter.execute("SET sql_mode = ''")

Since sql_mode can be a comma separated list of modes, ideally you probably want to do something smarter to just strip TRADITIONAL from the list, but the above is a quick and dirty solution.

UPDATE: It's not just the TRADITIONAL flag that causes it. It's TRADITIONAL,STRICT_ALL_TABLES and STRICT_TRANS_TABLES. I inspected what JDBC had configured and eliminated the problematic modes by trial and error. The resulting list that doesn't produce the WARNING = ERROR behaviour is:

REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,NO_UNSIGNED_SUBTRACTION,NO_DIR_IN_CREATE,ANSI,NO_BACKSLASH_ESCAPES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文