1

I recently looked at my schema.rb file and was alarmed to find that certain columns that do exist in my database do not appear, and some tables are missing entirely. The missing columns were added to the database through "def change add_column" migrations, though some columns that were added in that way do appear as expected in schema.rb.

On closer inspection, I realized that schema.rb has not been updated since I created the Users table.

20151019205241_create_users.rb:

class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name t.string :email t.timestamps null: false end end end 

This has not caused an issue for me in practice, but I thought schema.rb was supposed to be kept automatically updated, and that it would be important to have it updated in order to recreate the database. Can anyone help me figure out why it would not be updating?

One possibility is that it stopped being updated when I switched my database from sqlite3 to postgresql. I don't remember exactly, but I think the timing makes sense.

2
  • Not sure how switching the database interferes with schema.rb. Does the file get updated correctly when you do a rake db:migrate now? (You don't need to add any new migrations for this Rake task to run successfully.) Commented Aug 15, 2016 at 21:32
  • It did not. Next time I update the database in some way I will test this out. For now @smefju's solution below worked. Thanks for your reply! You're right, it does not appear to have anything to do with switching databases. Commented Aug 16, 2016 at 19:43

1 Answer 1

6

You can use rake db:schema:dump to re-create the file from the current database structure.

Schema file is independent from the driver so migrating from sqlite3 to PostgreSQL shouldn't matter. Make sure that version for definition in the file is not greater than current the current date. You can also add the whole file to the question's snippet.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.