Reset the database (purge all), then seed a database

后端 未结 6 913
感情败类
感情败类 2021-01-29 18:09

Is there a rake command to wipe out the data in the database tables?

How do I create a db:seed script to pre-fill data to my tables?

6条回答
  •  不知归路
    2021-01-29 18:16

    on Rails 6 you can now do something like

    rake db:seed:replant This Truncates tables of each database for current environment and loads the seeds

    https://blog.saeloun.com/2019/09/30/rails-6-adds-db-seed-replant-task-and-db-truncate_all.html

    $ rails db:seed:replant --trace
    ** Invoke db:seed:replant (first_time)
    ** Invoke db:load_config (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Execute db:load_config
    ** Invoke db:truncate_all (first_time)
    ** Invoke db:load_config
    ** Invoke db:check_protected_environments (first_time)
    ** Invoke db:load_config
    ** Execute db:check_protected_environments
    ** Execute db:truncate_all
    ** Invoke db:seed (first_time)
    ** Invoke db:load_config
    ** Execute db:seed
    ** Invoke db:abort_if_pending_migrations (first_time)
    ** Invoke db:load_config
    ** Execute db:abort_if_pending_migrations
    ** Execute db:seed:replant
    

提交回复
热议问题