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