简体   繁体   中英

Generate the schema.rb from Rails Models (without Database)

I have a Rails project that has an outdated schema.rb file. I also have an empty database. How do I generate the schema.rb from the Rails Models?

You shouldn't be generating a schema from your models, but rather your database or migration files. If your migrations are up to date and reflect the state of your database, you can run:

bundle exec rake db:schema:dump 

It should generate the schema.rb file from your database.

This is what Rails runs after the db:migrate task has executed:

Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby

你可以试试;

rake db:schema:load

The ActiveRecord models don't store any information about the database schema. The schema is stored in the schema.rb file.

The file is generated from the database, at the end of every migration. Therefore, if you have an up-to-date database, simply run:

$ rake db:migrate

to sync the schema. If you have an empty database, then there is no way to update the schema (actually, at that point I wonder what your Rails models are using).

The only thing you can do is:

  1. load the current schema and migrate from that
  2. reuse the migrations, assuming they are up to date

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