# legacy database with column named 'type'

**URL:** <https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623>\
**Category:** rubyonrails-talk\
**Created:** [July 28, 2009, 3:58pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623 "2009-07-28T15:58:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Craig\_White2](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@Craig\_White2](https://discuss.rubyonrails.org/u/Craig_White2)\
**Post date:** [July 28, 2009, 3:58pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/1 "2009-07-28T15:58:17Z")

</div>

I have a problem with a table that has a field named 'type'

When I try to use active record in a console, I get this error...

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: '10'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Debtortrans.inheritance\_column to use another column for that information.

Since I can't rename the column because the software creating it needs this, is it possible to alias the name somehow within rails, perhaps in the model?

Craig

---

<div class="post-metadata">

**Author:** ![rab](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/rab/32/710_2.png) [@rab](https://discuss.rubyonrails.org/u/rab)\
**Post date:** [July 28, 2009, 4:21pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/2 "2009-07-28T16:21:43Z")

</div>

The default value for the inheritance\_column is "type" so you just need to change that to something else and then you should be able to use 'type' normally for your legacy data.

class Debtortrans &nbsp;&nbsp;&nbsp;inheritance\_column :not\_used end

(Assuming that there isn't a column named "not\_used" in that table, of course. 😉

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com) Rob@AgileConsultingLLC.com

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [July 28, 2009, 4:43pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/3 "2009-07-28T16:43:23Z")

</div>

Is there another way using some sort of alias on the column name as the OP suggested? I have a legacy db with a column called action and suspect that this may cause me some problems at some point. Maybe it will not however.

Colin

---

<div class="post-metadata">

**Author:** ![Craig\_White2](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@Craig\_White2](https://discuss.rubyonrails.org/u/Craig_White2)\
**Post date:** [July 28, 2009, 5:24pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/4 "2009-07-28T17:24:05Z")

</div>

> \> \> I have a problem with a table that has a field named 'type' \> \> When I try to use active record in a console, I get this error... \> \> ActiveRecord::SubclassNotFound: The single-table inheritance mechanism \> failed to locate the subclass: '10'. This error is raised because the \> column 'type' is reserved for storing the class in case of  
> \> inheritance. \> Please rename this column if you didn't intend it to be used for  
> \> storing \> the inheritance class or overwrite Debtortrans.inheritance\_column to  
> \> use \> another column for that information. \> \> Since I can't rename the column because the software creating it needs \> this, is it possible to alias the name somehow within rails, perhaps  
> \> in \> the model? \> \> Craig
> 
> The default value for the inheritance\_column is "type" so you just  
> need to change that to something else and then you should be able to  
> use 'type' normally for your legacy data.
> 
> class Debtortrans &nbsp;&nbsp;&nbsp;inheritance\_column :not\_used end
> 
> (Assuming that there isn't a column named "not\_used" in that table, of  
> course. 😉

---

<div class="post-metadata">

**Author:** ![rab](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/rab/32/710_2.png) [@rab](https://discuss.rubyonrails.org/u/rab)\
**Post date:** [July 28, 2009, 5:29pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/5 "2009-07-28T17:29:19Z")

</div>

If it's because you see "action" in this page:

[http://wiki.rubyonrails.org/rails/pages/ReservedWords](http://wiki.rubyonrails.org/rails/pages/ReservedWords)

(or a similar one), then it is probably within a controller. You shouldn't have problems with 'action' as a column/attribute name on a model.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com) Rob@AgileConsultingLLC.com

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [July 28, 2009, 7:34pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/6 "2009-07-28T19:34:53Z")

</div>

I admit I have not run into a problem yet, I suppose I am concerned that something like :conditions =\> {:action =\> value} might confuse the system. I suppose if it did I could avoid the use of :action by using ['action = ?', value] instead.

I will not worry unless it becomes a problem. Thanks for the help.

Colin

---

<div class="post-metadata">

**Author:** ![Zach\_Karpinski](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@Zach\_Karpinski](https://discuss.rubyonrails.org/u/Zach_Karpinski)\
**Post date:** [July 28, 2009, 7:48pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/7 "2009-07-28T19:48:01Z")

</div>

I had a similar problem when I hooked my Wordpress installation into my rails site. On the wp\_posts table they use ID as their primary key. Ruby/Rails was getting unhappy whenever I tried to access obj.ID. I was able to access this field by using obj.attributes ["ID"].

I don't know if this will work for you without a code example, but its worth a shot.

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [July 28, 2009, 8:31pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/8 "2009-07-28T20:31:51Z")

</div>

[Inheritance\_column issues - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/101557) suggests that just changing the inheritance column name may not be enough, it may also be necessary to provide access methods as type is a reserved word. The link suggests a solution.

Colin

---

<div class="post-metadata">

**Author:** ![Craig\_White2](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@Craig\_White2](https://discuss.rubyonrails.org/u/Craig_White2)\
**Post date:** [July 28, 2009, 11:17pm UTC](https://discuss.rubyonrails.org/t/legacy-database-with-column-named-type/44623/9 "2009-07-28T23:17:12Z")

</div>

> \> \> \>\> \>\> I have a problem with a table that has a field named 'type' \>\> \>\> When I try to use active record in a console, I get this error... \>\> \>\> ActiveRecord::SubclassNotFound: The single-table inheritance mechanism \>\> failed to locate the subclass: '10'. This error is raised because the \>\> column 'type' is reserved for storing the class in case of \>\> inheritance. \>\> Please rename this column if you didn't intend it to be used for \>\> storing \>\> the inheritance class or overwrite Debtortrans.inheritance\_column to \>\> use \>\> another column for that information. \>\> \>\> Since I can't rename the column because the software creating it needs \>\> this, is it possible to alias the name somehow within rails, perhaps \>\> in \>\> the model? \>\> \>\> Craig \> \> \> The default value for the inheritance\_column is "type" so you just \> need to change that to something else and then you should be able to \> use 'type' normally for your legacy data. \> \> class Debtortrans \> inheritance\_column :not\_used \> end \> \> (Assuming that there isn't a column named "not\_used" in that table, of \> course. 😉\>
> 
> [Inheritance\_column issues - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/101557) suggests that just changing the inheritance column name may not be enough, it may also be necessary to provide access methods as type is a reserved word. The link suggests a solution.
