Topic: Is "class" considered an invalid database column name?

I recently started messing around with Rails, and one of the first apps I tried writing had a database column called 'class'.  Now when I try to output the thing in a view, like <%= modelvar.class %> it displays the name of the Model rather than the value of the database column, not surprisingly.

Is this name considered invalid, or is there a better way to access it?

Thanks.

Last edited by tsigo (2006-07-10 11:16:11)

Re: Is "class" considered an invalid database column name?

Yes, class is a reserved word.

Try modelvar['class']

Re: Is "class" considered an invalid database column name?

That makes sense, thanks.

Re: Is "class" considered an invalid database column name?

There are quite a few names that can't be used as columns, either because of Ruby or SQL or ActiveRecord.  I've seen a highly experienced programmer fall foul of a column that he'd called 'type'.  Rails didn't appear to mind but MySQL objected when you tried to Rake the database.  It pays to avoid anything that you think might be reserved by one or other part of the stack.

Re: Is "class" considered an invalid database column name?

This list of reserved words might help.

http://wiki.rubyonrails.com/rails/pages/ReservedWords

allen wrote:

I've seen a highly experienced programmer fall foul of a column that he'd called 'type'.  Rails didn't appear to mind but MySQL objected when you tried to Rake the database.

I'm surprised MySQL would complain about the column 'type' because it's not one of the reserved words. However, there is an interesting comment about naming things 'type' on this page, although it doesn't really say much.

http://dev.mysql.com/doc/refman/5.0/en/ … words.html

I would think calling a column 'type' would be necessary because Rails does some magic with it and Single Table Inheritance/Polymorphic relation.

Railscasts - Free Ruby on Rails Screencasts