Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Binary table default values

#1273

I have an existing database, with a schema that has binary columns, and a default value for those columns (in this case "0000000000000000"). I am trying to extract the schema using "rake --trace db:schema:dump", and am getting the following error: ** Execute db:schema:dump rake aborted! binary columns cannot have a default value: "0000000000000000" Clearly it does not like default binary values.

I was told that this is in fact bug; and not a limitation of ROR. If anyone has any advice on a workaround, I would appreciate it.

Reported by Oliver Friedrichs · October 26th, 2008 @ 06:12 PM

State: committed
Milestone: 2.3.4
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to incomplete

    You should try to submit a failing test case. http://guides.rails.info/contrib... would come handy :)

    Thanks.

    March 10th, 2009 @ 12:47 PM

  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from binary, database, tables to binary, bugmash, database, tables

    August 5th, 2009 @ 03:26 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • State changed from incomplete to invalid
    • Tag changed from binary, bugmash, database, tables to binary, database, tables
    • Milestone cleared.

    Correct: MySQL does not permit default values for BLOB or TEXT columns. You can provide one, but MySQL will silently ignore it.

    Workaround: provide your own default when you create new records using a before_create callback.

    August 10th, 2009 @ 02:57 AM

  4. Jatinder Singh
    Jatinder Singh

    Correct MySQL doesn't permit default values for BLOB or TEXT columns. but MySQL has BINARY and VARBINARY types(which support default values), and AR considers them as as type "binary". here is relevant piece of code from schema_definitions.rb,

            def simplified_type(field_type)
              case field_type
                when /int/i
                  :integer
                when /float|double/i
                  :float
                when /decimal|numeric|number/i
                  extract_scale(field_type) == 0 ? :integer : :decimal
                when /datetime/i
                  :datetime
                when /timestamp/i
                  :timestamp
                when /time/i
                  :time
                when /date/i
                  :date
                when /clob/i, /text/i
                  :text
                when /blob/i, /binary/i
                  :binary
                when /char/i, /string/i
                  :string
                when /boolean/i
                  :boolean
              end
            end
    

    I think its right for AR to consider BINARY and VARBINARY as BINARY type, and utilize methods like string_to_binary with them. The only difference I see is with default values. here is a potential fix for the problem, http://gist.github.com/164981. let me know how it looks and I can create a patch for it.

    August 10th, 2009 @ 03:10 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • State changed from invalid to open
    • Milestone set to 2.3.4

    Indeed. The fix looks good!

    August 10th, 2009 @ 03:14 AM

  6. Jatinder Singh
    Jatinder Singh

    I'll create a patch for this fix and missing tests for MySQLColumn.

    August 10th, 2009 @ 03:17 AM

  7. Jatinder Singh
    Jatinder Singh
    • Tag changed from binary, database, tables to binary, bugmash, database, tables

    August 10th, 2009 @ 03:22 AM

  8. Jatinder Singh
    Jatinder Singh
    • Tag changed from binary, bugmash, database, tables to activerecord, binary, bugmash, database, mysql, tables

    I've attached a patch with tests for the fix for master. Patch for 2.3-stable coming up next.

    August 10th, 2009 @ 04:51 AM

  9. Jatinder Singh
    Jatinder Singh
    • Tag changed from activerecord, binary, bugmash, database, mysql, tables to activerecord, binary, bugmash, database, mysql, patch, tables

    August 10th, 2009 @ 04:53 AM

  10. Jatinder Singh
    Jatinder Singh

    I just realized that the same patch gets applied to 2-3-stable successfully.

    August 10th, 2009 @ 05:00 AM

  11. Repository
    Repository
    • State changed from open to committed

    (from [8a49af31581095a35b6dc8b20ed12c2f9fb80855]) AR should respect default values for MySQL BINARY and VARBINARY columns.

    [#1273 Binary table default values state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/8a49af31581095a35b6dc8b20ed12c...

    August 10th, 2009 @ 05:06 AM

  12. Repository
  13. Jeremy Kemper
    Jeremy Kemper
    • Tag changed from activerecord, binary, bugmash, database, mysql, patch, tables to activerecord, binary, database, mysql, patch, tables

    August 10th, 2009 @ 05:06 AM