This project is archived and is in readonly mode.

#1742 ✓wontfix
Michael Shapiro

[PATCH] serialize fails when :type => :text and :null => false with mysql only

Reported by Michael Shapiro | January 12th, 2009 @ 11:00 PM | in 2.x

It seems that using :null => false in the migration causes serialize to fail, removing it fixes the problem.

The migration

class CreateTables < ActiveRecord::Migration public def self.up

create_table :blargs do |t|
  t.text  :field, :null => false
end

end

def self.down

drop_table :blargs

end end

The class

class Blarg < ActiveRecord::Base serialize :field, Hash end

The script

a = Blarg.new a.field = {} p a.save

The results

/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:299:in unserialize_attribute': field was supposed to be a Hash, but was a String (ActiveRecord::SerializationTypeMismatch)

Comments and changes to this ticket

  • Michael Shapiro

    Michael Shapiro January 12th, 2009 @ 11:00 PM

    • Title changed from “serilize fails when :null => false” to “serialize fails when :null => false”
  • Matt Stuart

    Matt Stuart March 6th, 2009 @ 09:40 PM

    It should be noted that the following also fails (using Blarg as described above)

    h = { :foo => "bar" } a = Blarg.new( h ) /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:299:in unserialize_attribute': field was supposed to be a Hash, but was a String (ActiveRecord::SerializationTypeMismatch)

    Current work around is to remove the :null => false from the migration

  • CancelProfileIsBroken

    CancelProfileIsBroken August 5th, 2009 @ 02:58 PM

    • Tag changed from 2.2.2, serialize to 2.2.2, bugmash, serialize
  • Elise Huard

    Elise Huard August 9th, 2009 @ 12:07 AM

    verified on edge with mysql and ruby 1.8.6
    I even get the error at Blarg.new, when :null => false. When :null => false is gone, then the given example works.

    >> b = Blarg.new
    ActiveRecord::SerializationTypeMismatch: field was supposed to be a Hash, but was a String
    
  • Elad Meidar

    Elad Meidar August 9th, 2009 @ 01:49 AM

    +1 verified on 2-3-stable, mysql and ruby 1.8.6, got the error too on Blarg.new when :null => false is present.

  • Elad Meidar
  • Elad Meidar

    Elad Meidar August 9th, 2009 @ 02:51 AM

    For master (patch cant' be applied) i get:

    >> a = Blarg.new
    => #<Blarg id: nil, field: nil, created_at: nil, updated_at: nil>
    >> a.field = {:elad => 'elad'}
    ArgumentError: wrong number of arguments (4 for 1)

    from /Users/eladmeidar/projects/bugmash/vendor/rails/activerecord/lib/../../activemodel/lib/active_model/attribute_methods.rb:147:in `attribute_changed?'
    from /Users/eladmeidar/projects/bugmash/vendor/rails/activerecord/lib/../../activemodel/lib/active_model/attribute_methods.rb:147:in `send'
    from /Users/eladmeidar/projects/bugmash/vendor/rails/activerecord/lib/../../activemodel/lib/active_model/attribute_methods.rb:147:in `field_changed?'
    from /Users/eladmeidar/projects/bugmash/vendor/rails/activerecord/lib/active_record/attribute_methods/dirty.rb:153:in `write_attribute'
    from /Users/eladmeidar/projects/bugmash/vendor/rails/activerecord/lib/active_record/attribute_methods/write.rb:13:in `field='
    from /Users/eladmeidar/projects/bugmash/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:35:in `send'
    from /Users/eladmeidar/projects/bugmash/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:35:in `method_missing'
    from (irb):3
    
    
    
    
    >>
  • Kieran P

    Kieran P August 9th, 2009 @ 04:51 AM

    Unable to reproduce on Rails 2.3 with SQLite, but I get the same error at Elad for Rails 3.0 (master) with SQLite

  • Kieran P

    Kieran P August 9th, 2009 @ 07:05 AM

    • Title changed from “serialize fails when :null => false” to “[PATCH] serialize fails when :null => false”
  • Rizwan Reza

    Rizwan Reza August 9th, 2009 @ 04:16 PM

    verified

    +1 The patch applies cleanly though, there are no tests.

  • David Trasbo

    David Trasbo August 9th, 2009 @ 07:16 PM

    verified

    Bug verified but the patch does not apply to edge and needs tests.

  • Hugo Peixoto

    Hugo Peixoto August 9th, 2009 @ 11:49 PM

    verified on master. Using the migration given, just doing Blarg.create() yields the errors posted by Elad.

  • Joe Van Dyk

    Joe Van Dyk September 26th, 2009 @ 11:08 PM

    Can't reproduce this bug on 2-3-stable.

  • Joe Van Dyk

    Joe Van Dyk September 26th, 2009 @ 11:12 PM

    • Tag changed from 2.2.2, bugmash, serialize to 3, bugmash, serialize

    Also can't reproduce on 2-2-stable with sqlite.

    I can reproduce on master w/ same error as elad though.

  • rbxbx

    rbxbx September 26th, 2009 @ 11:20 PM

    -1 Can't reproduce on 2-3-stable.. didn't bother applying the patch as there seemed to be no reason to.

  • Joe Van Dyk

    Joe Van Dyk September 27th, 2009 @ 12:33 AM

    I have a feeling 'field' might be a reserved column name in 3.0. I can't reproduce the problem if a different column name is used.

  • Joe Van Dyk

    Joe Van Dyk September 27th, 2009 @ 12:58 AM

    I created #3274 for the 'field' column name bug.

  • Blue Box Chris
  • Elad Meidar

    Elad Meidar September 27th, 2009 @ 04:00 AM

    +1 on Joe's comment in here as well, once changing the name of the field to something else, issue vanished. On the other hand, i was unable to find any appearance of a method, symbol or object named 'field'... so i have no idea if this is a reserved word or not.

  • Akira Matsuda

    Akira Matsuda September 27th, 2009 @ 08:50 AM

    -1 Can't reproduce on 2-3-stable with a different column name as Joe pointed out

  • CancelProfileIsBroken

    CancelProfileIsBroken September 27th, 2009 @ 11:37 AM

    • State changed from “new” to “wontfix”
    • Tag changed from 3, bugmash, serialize to 3, serialize

    Looks like we should let #3274 handle this, rather than patching the symptom. Closing.

  • Sylvain R

    Sylvain R July 29th, 2010 @ 01:30 PM

    • Tag changed from 3, serialize to 3, serialize
    • Importance changed from “” to “”

    I confirm that this bug is still existing today as of version 2.3.8 (as well as 2.3.5 which I am using).

    The issue is not the field name (though the example uses this weird field name 'field' which happen to be also an issue somewhere else, but this was a coincidence IMHO). The problem still exists when using a "normal" field name.

    In short: "serialize :the_field_name, Hash" won't work if:

    • using MySQL (I am using MySQL 5.1), as I don't have any issue with sqlite.
    • the field has :null => false.
    • the field's type is :text (no problem with :type => :string).

    To reproduce try with the following:

    db/migrate/20100729121113_create_cars.rb

    class CreateCars < ActiveRecord::Migration
      def self.up
        create_table :cars do |t|
          t.string :brand, :null => false
          t.text :model_name, :null => false
    
          t.timestamps
        end
      end
    
      def self.down
        drop_table :cars
      end
    end
    

    app/models/car.rb

    class Car < ActiveRecord::Base
      serialize :model_name, Hash
    end
    

    In database.yml, make sure you are using mysql (no problem with sqlite).

    development:
      adapter: mysql
      encoding: utf8
      database: temp_development
      username: foo
      password: bar
    

    db/seeds.rb

    car = Car.create!(:brand => "Renault", :model_name => {"fr" => "Clio", "us" => "Foo"})
    

    finally we can try to run the seed:

    sr@sr-desktop:/tmp/apperror$ rake db:seed --trace
    (in /tmp/apperror)
    ** Invoke db:seed (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Execute db:seed
    rake aborted!
    model_name was supposed to be a Hash, but was a String
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/attribute_methods.rb:303:in `unserialize_attribute'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/attribute_methods.rb:275:in `read_attribute'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:3181:in `send'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:3181:in `clone_attribute_value'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/dirty.rb:134:in `write_attribute'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/attribute_methods.rb:1:in `model_name='
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `send'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `assign_attributes'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `each'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `assign_attributes'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2775:in `attributes='
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2473:in `initialize'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1069:in `new'
    /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1069:in `create!'
    /tmp/apperror/db/seeds.rb:1
    /var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:145:in `load_without_new_constant_marking'
    /var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:145:in `load'
    /var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:521:in `new_constants_in'
    /var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:145:in `load'
    /var/lib/gems/1.8/gems/rails-2.3.8/lib/tasks/databases.rake:211
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
    /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
    /var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
    /var/lib/gems/1.8/gems/rake-0.8.7/bin/rake:31
    /var/lib/gems/1.8/bin/rake:19:in `load'
    /var/lib/gems/1.8/bin/rake:19
    

    Here is the error "model_name was supposed to be a Hash, but was a String".

    • If you try with sqlite adapter, there is no problem at all.
    • If you set the 'model_name' field to :string type (in migrate file), no problem.
    • With :null => true, no problem either.

    FYI I am running ruby 1.8.7 on Linux.

  • Sylvain R

    Sylvain R July 29th, 2010 @ 01:35 PM

    • Title changed from “[PATCH] serialize fails when :null => false” to “[PATCH] serialize fails when :type => :text and :null => false with mysql only”
  • Kai Schlamp

    Kai Schlamp December 21st, 2010 @ 05:56 PM

    Why was this bug marked as "wontfix"?
    I can also confirm this bug with Rails 3.0.3, Ruby 1.8.7 and MySQL 5.1.

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Tags

Referenced by

Pages