简体   繁体   中英

How to create database from schema.rb without initializing Rails?

I am trying to create all my tables from schema.rb

I used the command: "rake db:schema:load"

However, this fails because in one of my initializers, it is referencing a model/table that obviously doesn't exist (since the database is empty)

I could comment out these lines, and then run schema:load again, but is there an alternative?

Probably the fastest way is to just move the offending initializer to a temporary directory that is outside of the app, and then run your schema load. But if that doesn't work, or isn't an option for some reason, you could always work around that by creating a bare bones rails app to do the schema load:

  1. Create a new rails app: rails new (app)-fixer
  2. Copy your gemfile (unless there are specific exceptions) to the fixer app.
  3. Copy your database.yml config to the fixer app.
  4. Copy your schema.rb file to the fixer app.
  5. Do all appropriate "bundle install" commands as needed for your app.
  6. Then run "rake db:drop db:create db:schema:load"

That will build up a new database from scratch, based on your current schema.

You can add a check for the table existance in your initializer.

if TheModel.table_exists?
  // do something with the model
end

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