#1092 ✓not-applicable

Integer properties cannot be set to a blank string when using MySQL

Reported by phatmann | October 12th, 2009 @ 02:16 PM

In DM 0.10.1, if you pass a blank string into an Integer property, you get the following exception when using MySQL:

DataObjects::SQLError: Incorrect integer value: '' for column 'num' at row 1

Here is the code that shows the issue:


The causes major problems when submitting forms in Merb, since blank fields are passed as in as blank string parameter values, and these parameter values are generally passed right into a Resource#update call. This problem did not occur in 0.9.

Comments and changes to this ticket

  • phatmann

    phatmann October 12th, 2009 @ 02:19 PM

    Here is a proposed fix. In Datamapper::Property I added these lines at the head of typecast_to_numeric:

    def typecast_to_numeric(value, method)
          if value.is_a?(String) && value.strip.empty?

    I can submit a patch and spec as needed.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) October 24th, 2009 @ 04:27 AM

    • State changed from “new” to “not-applicable”

    I don't think that dm-core should silently convert a blank string to nil like this. It might be possible that a String should trigger a validation error, but because we converted it, we've lost the data. Maybe we want to treat nil differently from empty String in the validations. DM only converts a String to another primitive if it is an exact match, eg: "1" -> 1

    I would tend to perform this sort of thing at the controller level, since it's primary responsibility is to route the proper information to the models. I've done this before in Merb and Rails and it is relatively simple to add in.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) October 24th, 2009 @ 04:28 AM

    One other thing I should mention is that in edge dm-core, DM will not save anything in this situation. If you've passed in a value that is not coercable because it is invalid, Resource#save (which Resource#update uses) will not send the request to the adapter and instead return false.

  • phatmann

    phatmann October 26th, 2009 @ 11:46 AM

    Here is an alternate suggestion: add a :blank_value property option. When false, blank strings are rejected. When nil, blanks are mapped to nil. When 0, blanks are mapped to 0. The default would be false.

  • MarkMT

    MarkMT October 26th, 2009 @ 04:09 PM

    Making false the default may make sense for an integer property, but it probably doesn't for a string ... which maybe suggests that this isn't the best way to handle this problem. Like Dan I've dealt with this in the past in the controller, e.g. something like attr.delete_if {|k,v| v.empty?}

  • Tim Connor

    Tim Connor October 1st, 2010 @ 12:23 PM

    • Milestone order changed from “0” to “0”

    So Datamapper is in favor of silent failures with no coercion or feedback of any sort in support of being academically purer than ActiveRecord (or any of the other ruby ORMS)?

    I can understand the stance of not wanting to coerce to a zero, and support that, but you must either coerce, raise an error, of mark as invalid. Silently failing to save is a completely untenable ivory tower solution that I don't think any single person actually building apps would agree with.

    Sorry for my flame, but this just seriously bit me on an app that is in the final stretch for client hand-off, and the general approach of supporting silent failures in DM has totally turned me off the project.

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) October 1st, 2010 @ 12:46 PM

    There is a plan to add exception raising in such and similar situations. I agree it's a PITA that DM silently ignores invalid values. Good news though is that with dm-validations included it would end up with a validation error.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »