简体   繁体   中英

Does Rails' schema.rb always show all indexes?

I was adding a few multi-column indexes to our production database in Rails 3.2.14 yesterday, and when I ran the migration it failed with an error indicating that the requested index already existed. However, it wasn't in the schema.rb file. I found this a little baffling. I didn't create the indexes and I can't find a migration creating them.

The development database for this app is SQLite, but the production database is MySQL. Is it possible that the index was created in a development migration which ran in development but was edited/rolled back before going to production, and SQLite didn't remove the index? Does schema.rb always show all indexes, or just ones explicitly added by migrations?

According to the Rails Guides, the answer is yes. Schema.rb reflects tables, column and indices in your database, but keep in mind it is only updated when you run most of rake commands in the db namespace.

If :ruby is selected then the schema is stored in db/schema.rb. If you look at this file you'll find that it looks an awful lot like one very big migration:

[Schema example]

In many ways this is exactly what it is. This file is created by inspecting the database and expressing its structure using create_table, add_index, and so on. Because this is database-independent, it could be loaded into any database that Active Record supports. This could be very useful if you were to distribute an application that is able to run against multiple databases.

Source: http://guides.rubyonrails.org/migrations.html#types-of-schema-dumps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM