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.

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

#1742

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)

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

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Michael Shapiro
    Michael Shapiro
    • Title changed from serilize fails when :null => false to serialize fails when :null => false

    January 12th, 2009 @ 11:00 PM

  2. Matt Stuart
    Matt Stuart

    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

    March 6th, 2009 @ 09:40 PM

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

    August 5th, 2009 @ 02:58 PM

  4. Elise Huard
    Elise Huard

    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
    

    August 9th, 2009 @ 12:07 AM

  5. Elad Meidar
    Elad Meidar

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

    August 9th, 2009 @ 01:49 AM

  6. Elad Meidar
  7. Elad Meidar
    Elad Meidar

    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
    
    
    
    
    >>

    August 9th, 2009 @ 02:51 AM

  8. Kieran P
    Kieran P

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

    August 9th, 2009 @ 04:51 AM

  9. Kieran P
    Kieran P
    • Title changed from serialize fails when :null => false to [PATCH] serialize fails when :null => false

    August 9th, 2009 @ 07:05 AM

  10. Rizwan Reza
    Rizwan Reza

    verified

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

    August 9th, 2009 @ 04:16 PM

  11. David Trasbo
    David Trasbo

    verified

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

    August 9th, 2009 @ 07:16 PM

  12. Hugo Peixoto
    Hugo Peixoto

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

    August 9th, 2009 @ 11:49 PM

  13. Joe Van Dyk
    Joe Van Dyk

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

    September 26th, 2009 @ 11:08 PM

  14. Joe Van Dyk
    Joe Van Dyk
    • 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.

    September 26th, 2009 @ 11:12 PM

  15. rbxbx
    rbxbx

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

    September 26th, 2009 @ 11:20 PM

  16. Joe Van Dyk
    Joe Van Dyk

    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.

    September 27th, 2009 @ 12:33 AM

  17. Joe Van Dyk
  18. Blue Box Chris
  19. Elad Meidar
    Elad Meidar

    +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.

    September 27th, 2009 @ 04:00 AM

  20. Akira Matsuda
    Akira Matsuda

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

    September 27th, 2009 @ 08:50 AM

  21. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to wontfix
    • Tag changed from 3, bugmash, serialize to 3, serialize

    Looks like we should let #3274 When an ActiveRecord has a column named 'field', bad behavior happens handle this, rather than patching the symptom. Closing.

    September 27th, 2009 @ 11:37 AM

  22. Sylvain R
    Sylvain R
    • 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.

    July 29th, 2010 @ 01:30 PM

  23. Sylvain R
    Sylvain R
    • Title changed from [PATCH] serialize fails when :null => false to [PATCH] serialize fails when :type => :text and :null => false with mysql only

    July 29th, 2010 @ 01:35 PM

  24. Kai Schlamp
    Kai Schlamp

    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.

    December 21st, 2010 @ 05:56 PM