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

**URL:** <https://www.ruby-forum.com/t/cant-add-data-during-migration-if-column-is-called-type/55335>\
**Category:** Rails\
**Created:** [March 20, 2006, 12:05am UTC](https://www.ruby-forum.com/t/cant-add-data-during-migration-if-column-is-called-type/55335 "2006-03-20T00:05:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 20, 2006, 12:05am UTC](https://www.ruby-forum.com/t/cant-add-data-during-migration-if-column-is-called-type/55335/1 "2006-03-20T00:05:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 20, 2006, 12:44am UTC](https://www.ruby-forum.com/t/cant-add-data-during-migration-if-column-is-called-type/55335/2 "2006-03-20T00:44:09Z")

</div>

type is a special field name used for STI.

Pat

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 20, 2006, 12:46am UTC](https://www.ruby-forum.com/t/cant-add-data-during-migration-if-column-is-called-type/55335/3 "2006-03-20T00:46:33Z")

</div>

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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [March 20, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/cant-add-data-during-migration-if-column-is-called-type/55335/4 "2006-03-20T17:47:42Z")

</div>

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