This project is archived and is in readonly mode.
[PATCH] serialize fails when :type => :text and :null => false with mysql only
-
Michael Shapiro
- Title changed from serilize fails when :null => false to serialize fails when :null => false
-
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
-
CancelProfileIsBroken
- Tag changed from 2.2.2, serialize to 2.2.2, bugmash, serialize
-
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 -
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.
-
Elad Meidar
Patch for 2-3-stable, passing tests.
-
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
>> -
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
-
Kieran P
- Title changed from serialize fails when :null => false to [PATCH] serialize fails when :null => false
-
Rizwan Reza
verified
+1 The patch applies cleanly though, there are no tests.
-
David Trasbo
verified
Bug verified but the patch does not apply to edge and needs tests.
-
Hugo Peixoto
verified on master. Using the migration given, just doing Blarg.create() yields the errors posted by Elad.
-
Joe Van Dyk
Can't reproduce this bug on 2-3-stable.
-
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.
-
rbxbx
-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
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
I created #3274 When an ActiveRecord has a column named 'field', bad behavior happens for the 'field' column name bug.
-
Blue Box Chris
+1 at Joe's comment
-
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.
-
Akira Matsuda
-1 Can't reproduce on 2-3-stable with a different column name as Joe pointed out
-
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.
-
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 endapp/models/car.rb
class Car < ActiveRecord::Base serialize :model_name, Hash endIn database.yml, make sure you are using mysql (no problem with sqlite).
development: adapter: mysql encoding: utf8 database: temp_development username: foo password: bardb/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:19Here 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
- Title changed from [PATCH] serialize fails when :null => false to [PATCH] serialize fails when :type => :text and :null => false with mysql only
-
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.
