SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    mouse monkey
    Join Date
    Dec 1999
    Location
    UK
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] MySql Reserved Words

    I originally started developing a ruby in rails with mysql 4. One of the table in my is called 'reads'. The wasn't a problem.

    Recently my host upgraded to mysql 5, so I installed mysql5 on my development environment. 'reads' has now been added to the reserved word list. I now get the following error:

    Code:
    Mysql::Error: #42000You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'reads' at line 1: SHOW FIELDS FROM reads
    If I was writing the sql myself I would just quote the table name, which would get round the problem but I don't have this control when abstracting the sql using rails.

    Any way of getting round this without changing my table name?

    Thanks.

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting problem, I'd have assumed that ActiveRecord would backtick everything.

    Any chance set_table_name "`reads`" works?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Luckily changing the table name in RoR isn't too big of a deal. You can just use the "set_table_name" method in your Read model and you don't have to change anything in the rest of your Rails code. The model name stays the same but you just tell it to use a different table name. (Just in case you don't find another way around renaming it. )
    Erh

  4. #4
    mouse monkey
    Join Date
    Dec 1999
    Location
    UK
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help!

    Code:
    set_table_name "`reads`"
    Worked a treat (I'm surprised rails doesn't do this automatically)

    Also good to know that renaming tables is simple.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its good to know that you can set backticks in the set_table_name method. I am also suprised that Rails doesn't do this automatically...
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Adding in here, I posted a new ticket on the Trac for Rails:
    http://dev.rubyonrails.org/ticket/4905#preview
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •