Ashley Moran
2006-Mar-19 23:05 UTC
[Rails] Can''t add data during migration if column is called "type"
Hi I''m new to Rails. This is my first application and I''m trying to set up a schema I''ve hand-crafted as a migration. Does anybody know why this... # deposit_types create_table :deposit_types do |table| table.column :type_code, :string, :limit => 20, :null => false table.column :description, :string, :limit => 50, :null => false end add_index :deposit_types, :type_code, :unique add_index :deposit_types, :description, :unique [ ["pc_of_tcp", "percentage of total cash price"], ["amount", "amount"], ["payments", "payments"] ].each do |type, description| DepositType.create :type_code => type, :description => description end works, but this... # deposit_types create_table :deposit_types do |table| table.column :type, :string, :limit => 20, :null => false table.column :description, :string, :limit => 50, :null => false end add_index :deposit_types, :type, :unique add_index :deposit_types, :description, :unique [ ["pc_of_tcp", "percentage of total cash price"], ["amount", "amount"], ["payments", "payments"] ].each do |type, description| DepositType.create :type => type, :description => description end (note change of type_code to type) The second migration fails because it tries to insert NULL into the type column: PGError: ERROR: null value in column "type" violates not-null constraint : INSERT INTO deposit_types ("type", "description") VALUES(NULL, ''percentage of total cash price'') I haven''t gone through the ActiveRecord code to see what''s going on. Is this a known gotcha? Is :type special somehow? I''ll stick with type_code for now. Thanks Ashley
Pat Maddox
2006-Mar-19 23:43 UTC
[Rails] Can''t add data during migration if column is called "type"
type is a special field name used for STI. Pat On 3/19/06, Ashley Moran <work@ashleymoran.me.uk> wrote:> Hi > > I''m new to Rails. This is my first application and I''m trying to set > up a schema I''ve hand-crafted as a migration. > > Does anybody know why this... > > # deposit_types > create_table :deposit_types do |table| > table.column :type_code, :string, :limit => 20, :null => false > table.column :description, :string, :limit => 50, :null => false > end > > add_index :deposit_types, :type_code, :unique > add_index :deposit_types, :description, :unique > > [ ["pc_of_tcp", "percentage of total cash price"], > ["amount", "amount"], > ["payments", "payments"] ].each do |type, description| > DepositType.create :type_code => type, :description => > description > end > > works, but this... > > # deposit_types > create_table :deposit_types do |table| > table.column :type, :string, :limit => 20, :null => false > table.column :description, :string, :limit => 50, :null => false > end > > add_index :deposit_types, :type, :unique > add_index :deposit_types, :description, :unique > > [ ["pc_of_tcp", "percentage of total cash price"], > ["amount", "amount"], > ["payments", "payments"] ].each do |type, description| > DepositType.create :type => type, :description => description > end > > (note change of type_code to type) > > The second migration fails because it tries to insert NULL into the > type column: > PGError: ERROR: null value in column "type" violates not-null > constraint > : INSERT INTO deposit_types ("type", "description") VALUES(NULL, > ''percentage of total cash price'') > > > I haven''t gone through the ActiveRecord code to see what''s going on. > Is this a known gotcha? Is :type special somehow? I''ll stick with > type_code for now. > > Thanks > > Ashley > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Josh Susser
2006-Mar-19 23:46 UTC
[Rails] Re: Can''t add data during migration if column is called "typ
Ashley Moran wrote:> I''m new to Rails. This is my first application and I''m trying to set > up a schema I''ve hand-crafted as a migration. > ... > Is this a known gotcha? Is :type special somehow? I''ll stick with > type_code for now.The "type" column name is used by Rails to indicated the class name in STI (Single Table Inheritance). You can change the inheritance column used for STI if you really want to, but it''s easier just to use a different name for your schema. This really outght to go in the newbie FAQ. Is there one of those yet on the wiki? --josh http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Ashley Moran
2006-Mar-20 16:47 UTC
[Rails] Re: Can''t add data during migration if column is called "typ
On Sunday 19 March 2006 23:46, Josh Susser wrote:> The "type" column name is used by Rails to indicated the class name in > STI (Single Table Inheritance). You can change the inheritance column > used for STI if you really want to, but it''s easier just to use a > different name for your schema. > > This really outght to go in the newbie FAQ. Is there one of those yet on > the wiki? > > --josh > http://blog.hasmanythrough.comCheers I agree a newbie FAQ would be good if there isn''t one. The Rails wiki is one of the most informative sites I''ve come across BUT it''s a bit disorganized and sometimes I end up cross-referencing dozens of pages to find what I want. Ashley