2

I have a large database with tons of tables and many indices on those tables. Every time I run a migration, the schema.rb file is generated fine, EXCEPT none of the indices are included. New tables and columns are added fine. If I manually re-add the add_index lines to the schema.rb file after the migration, it works fine, but doing this after every migration is a major hassle.

I've been dealing with this problem for months and haven't been able to find any solution or workaround. Does anyone know what might be happening?

Using Postgres & Rails 3.

D-Nice
  • 4,772
  • 14
  • 52
  • 86
  • you can try by generating a migration file just to add indeces to all your tables `add_index :users, [:email, :first_name, :last_name]` and so on..and run the migration – sa77 Nov 06 '16 at 01:56
  • It appears that the indices are already on the tables (when i connect via psql and run '\d table_name'). But for some reason it's just never getting written into the schema.rb file. – D-Nice Nov 06 '16 at 20:43
  • FWIW, I tried running a migration with add_index and the resulting schema.rb file that was generated still did NOT contain the indices. – D-Nice Nov 06 '16 at 21:34
  • I assume you've looked in `config/application.rb`, your initializers, and your environment files for anything that stands out? If not, you would be looking for something like `config.active_record.something_about_schema` – BigRon Dec 07 '16 at 04:00
  • 1
    @D-Nice It'd be easier to debug if we could see the schema and the migration. Are you using add_index or index: true? index: true works on references and belongs_to but not add_column. add_index should generally work. Can you share the schema and sample migrations? – Richard_G Dec 07 '16 at 10:31
  • @BigRon Yes, I've looked for these. Nothing suspicious in any of the project files – D-Nice Dec 11 '16 at 23:19
  • @R_G Well the problem is that the indices get wiped out regardless of what the migration is. I could be adding a new table with no add_index, and the new schema.rb would erase all the old indices. However, when I psql into the database I see that the indices actually exist. So it's as if Rails isn't seeing the postgres indices when it re-generates the schema.rb. – D-Nice Dec 11 '16 at 23:23
  • @D-Nice I know that this has been ask before but can you share 1 or 2 migrations and the schema.rb – MZaragoza Dec 12 '16 at 00:23
  • @D-Nice what happens when you do a `rake db:schema:dump`? can you check if that would create a schema with indices? – MZaragoza Dec 12 '16 at 00:25
  • Try copy pasting migration files in a fresh application and generate schema.rb – abhsss96 Dec 13 '16 at 09:49

1 Answers1

2

In Rails 3 indexes are not displayed in schema.rb file.

Given below is screenshot of shema dump from rails 3 documentation. No indexes are shown.

This is screenshot of schem file from rails documentation

I have tried to install Rails 3 with Postgres, Rails 3 with Mysql and Rails 3 and SQLite. All the indexes are added to database but you cannot see them in schema.rb.

There is only one way to show them in schema.rb by adding indexes manually to the file Or you can leave your scehma.rb without indexes. All the indexes are added to database when we run rake db:migrate. And if you want you can upgrade to rails 4 or above and you will never face this problem.

Neeraj Rana
  • 454
  • 4
  • 12