Can't add data during migration if column is called "type"


#1

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


#2

type is a special field name used for STI.

Pat


#3

Ashley M. 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


#4

On Sunday 19 March 2006 23:46, Josh S. 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.com

Cheers

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