Question

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

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top