4 ms·
I tried adding a partial index the other day but Rails didn't seem to particularly like it. Are the examples here from Rails 4? (I'm on 3.2.16). The issue I ha
by losvedir 13y ago
I tried adding a partial index the other day but Rails didn't seem to particularly like it. Are the examples here from Rails 4? (I'm on 3.2.16).
The issue I had was that, while the partial index syntax worked and actually did create it, it wasn't reflected anywhere in the generated schema. So I was worried that future developers wouldn't realize there was a partial index (we use schema.rb as the canonical reference of DB state), and if we ever get rid of migrations by condensing them into one big one we might miss that there should be a partial index on the column.
And changing the config to generate schema.sql instead of schema.rb didn't help. It's broken with the latest version of postgresql...
- ricny046 13y agoThe article covers only Rails 4.
- losvedir 13y agoAh, bummer. Anyone have any luck getting partial indexes to work in Rails 3.2?
- netghost 13y agoYou can always execute arbitrary SQL in migrations. For instance: def up execute('ALTER TABLE people ADD INDEX ...') end The one thing to keep in mind though is that if you use ruby schema format, your tests won't pick up those execute statements. In that case it's best to either use the sql format or re-run migrations in the test environment to set up the test db.
- lobster_johnson 13y agoYou could just do it manually: execute %{ create index index_users_on_name on users (name) where state = 'NY'; }
- srgpqt 13y agoUpgrading from Rails 3.2 to Rails 4.0 takes very little time, assuming the gems you depend on are compatible (which they should be, by now, or you might want to look for replacement gems..)
- twerquie 13y agoSwitch from schema.rb to structure.sql by setting config.active_record.schema_format = :sql in your application config if you want to do anything remotely interesting with your schema.