简体   繁体   中英

Rake db:setup does not run rails db:migrate, schema.rb does not exist error

On Rails 6, i expect rake db:setup to perform db:create , db:migrate and db:seed
i have only one data model and the record creation for its table was written in db/seeds.rb

# db/seeds.rb
require 'csv'

csv_text = File.read(Rails.root.join('lib', 'seeds', 'seed_first_table.csv'))
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1')
csv.each_with_index do |row, index|
  cr = CatProfile.new
  puts "row #{index} saved"
end

puts "There are now #{CatProfile.count} rows in the table"

database.yml also has a new user credential which was created through sudo -u postgres createuser -s new_user

But rake db:setup returns:

Created database 'my_application_development'
Created database 'my_application_test'
my_application/db/schema.rb doesn't exist yet. Run `rails db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /my_application/config/application.rb to limit the frameworks that will be loaded.

in sudo -u postgres psql it did make the databases but there are no relations (no seed table)

when I run rails db:migrate

== 20201103153526 CreateCatProfiles: migrating =================================
-- create_table(:cat_profiles)
   -> 0.0091s
== 20201103153526 CreateCatProfiles: migrated (0.0092s) ========================

then when I run rake db:setup a second time:

Database 'my_application_development' already exists
Database 'my_application_test' already exists
row 0 saved
row 1 saved 
...
row 1719 saved
There are now 1720 rows in the table

noticed that database already exists from the first rake db:setup but now it is able to seed the table

I dont understand why running rake db:setup the first time does not pick up necessary migrations before seeding

If you check out the source code for rails db:setup you will see that db:setup does NOT run db:migrate

Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)

db:schema:load loads what currently exists in your schema.rb into the database.

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