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.

Partial indices using add_index in DB migrations

#1333

Currently using Rails 2.1.1 with Postgres 8.3.1 and it doesn't seem like add_index provides the ability to create partial indices on database columns (same as a regular index but with a WHERE clause). Not sure if partial indicies can be created in other database systems but it would be nice to be able to have support for this as an option when using add_index.

My workaround is to add the index manually in an execute statement in my migration. Is this the recommended approach?

Reported by Jason Yuen · November 5th, 2008 @ 01:25 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Jason Yuen
    Jason Yuen
    • Title changed from Partial indicies using add_index in DB migrations to Partial indices using add_index in DB migrations

    November 5th, 2008 @ 02:05 PM

  2. Pratik
    Pratik
    • State changed from new to incomplete

    Could you please submit a patch for this ? http://guides.rails.info/contrib... should come handy.

    Thanks !

    March 12th, 2009 @ 05:28 PM

  3. Max Lapshin
    Max Lapshin

    Jason. It is very easy to monkey patch schema adaptor to add this functionality, but full patch is rather hard, because database driver must dump properly data of partial index.

    Take attached monkey patch and put it into config/initializers/ add_index :people, [:gender], :name => "male_idx", :options => "WHERE gender = 'm'"

    April 20th, 2009 @ 05:35 PM

  4. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from db, index, migrations, partial to bugmash, db, index, migrations, partial

    August 6th, 2009 @ 01:53 PM

  5. Jason Yuen
    Jason Yuen

    Just heard about BugMash and I would like to help out with this ticket. I was envisioning the same type of patch (adding options) but I noticed you (Max) mentioned that the full patch is rather hard. Can you please clarify what you mean by the database driver needing to dump data of the partial index? Thanks for clarifying!

    August 6th, 2009 @ 02:32 PM

  6. Elad Meidar
    Elad Meidar

    @Jason: as far as i can see (Google, that is), mysql does not support "where" clauses in CREATE INDEX syntax.. could it be that it's just a postgres's feature?

    September 27th, 2009 @ 08:57 PM

  7. Jason Yuen
    Jason Yuen

    Since the where clause would be added as an option, is it fair to say that create_index will simply generate an invalid statement if the optional parameters (WHERE clause or otherwise) are invalid for the particular database you are using? I see this as similar to calls to find_by_sql where, if you put DB-specific syntax, it's usually because you know that you are using a particular database.

    December 11th, 2009 @ 03:15 AM

  8. Rizwan Reza
    Rizwan Reza
    • Tag changed from bugmash, db, index, migrations, partial to db, index, migrations, partial

    February 12th, 2010 @ 12:46 PM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  10. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.

    Automatic cleanup of spam.

    November 8th, 2010 @ 08:27 AM

  11. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 9th, 2011 @ 12:31 AM

  12. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 9th, 2011 @ 12:31 AM