Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Limit MySQL index name length

#3508

The current MySQL adapter allows you to define new indices on a table with names that are generated beyond the allowed 50 byte length constraint:

add_index :posts, [:user_id, :a_long_column_name_that_concats_beyond_50]
# => "posts_on_user_id_and_a_long_column_name_that_concats_beyond_50"

This is an issue in a an environment where multiple databases are used in development vs production (sqlite vs postgresql vs mysql). Sqlite3 and PostgreSQL allow indices with longer names and therefore do not cause issue. But when MySQL is added, this problem may easily - and suddenly - show up.

Perhaps a logical truncation of the generated index name is appropriate in this case? Either way, since it is still around and hasn't been raised in Lighthouse, I thought it might be worth a revisit.

This issue was originally raised as ticket #10954 in Trac, two years ago by lesgrumels. It appears to still be an issue up to and through Rails 2.3.4.

Reported by Nathaniel Bibler · November 24th, 2009 @ 02:49 PM

State: duplicate
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Nathaniel Bibler
    Nathaniel Bibler

    Hrm.. the PostgreSQL adapter seems to have similar truncation logic to what I mentioned. See this ticket. It seems to truncate at 63 bytes (vs the 50 limit on MySQL).

    Perhaps it would be wise to just truncate all Rails-generated indices to 50 characters? Thereby making all ActiveRecord adapters act identically..

    November 24th, 2009 @ 03:04 PM

  2. Michael Sheakoski
    Michael Sheakoski

    I was able to create an index name 64 characters long on MySQL 5.0.51. Just letting you know so a fix for this issue doesn't make the name shorter than it needs to be.

    January 9th, 2010 @ 05:42 PM

  3. Nate Wiger
    Nate Wiger

    @Bibler Any limit needs to remain database dependent as Oracle's limit is 30 chars

    January 10th, 2010 @ 12:14 AM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to duplicate

    duplicate #1852

    April 11th, 2010 @ 09:08 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • State changed from duplicate to open

    Santiago, this is a limit on the length of the index name.

    April 22nd, 2010 @ 08:26 PM

  6. Santiago Pastorino
    Santiago Pastorino

    Sorry my bad here, i should read a bit slowly the next time.

    April 23rd, 2010 @ 01:09 AM

  7. Shajith Chacko
    Shajith Chacko

    I ran into this a while ago.

    There is a patch in #3452 which was mentioned in #3252 linked above. Is that good to go?

    April 23rd, 2010 @ 08:28 AM

  8. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to duplicate

    April 23rd, 2010 @ 06:04 PM

  9. Gustavo Delfino
    Gustavo Delfino
    • Importance changed from to

    I was just affected my this bug. As this has been marked as duplicate, I would like to know the new ticket number.

    July 24th, 2010 @ 04:10 PM

  10. rcrogers
    rcrogers

    I just ran into this in Rails 2.3.8. It took me quite a while to figure out what was going on.

    November 22nd, 2010 @ 06:20 PM

  11. Paul Eipper
    Paul Eipper

    This is a duplicate of which ticket?

    February 4th, 2011 @ 08:04 PM