Forum: Ruby on Rails Can't add data during migration if column is called "type"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ashley M. (Guest)
on 2006-03-20 01:05
(Received via mailing list)
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 M. (Guest)
on 2006-03-20 01:44
(Received via mailing list)
type is a special field name used for STI.

Pat
Josh S. (Guest)
on 2006-03-20 01:46
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
Ashley M. (Guest)
on 2006-03-20 18:47
(Received via mailing list)
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
This topic is locked and can not be replied to.