开发者

Rails 3: DataObjects::SQLError -- How do I stop these from producing errors on *warnings*?

开发者 https://www.devze.com 2023-03-03 12:06 出处:网络
When I insert just a few fields to a table in a legacy database schema (via DataMapper), I\'m getting the following:

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号