문제

Hello I am new to rails. I am working with an existing postgres db. I would like to create CRUD for all of the tables. I've started with: rails g scaffold firstTableName

Then I start rails: rails s

When I navigate to localhost:3000/firstTableName, rails gives the following error: Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.

I thought that 'rake db:migrate' was only for setting up your database. My db is already set up. If I run 'rake db:migrate', then I get an error that firstTableName already exists. Please Help me understand how to make rails happy. Thank you.

도움이 되었습니까?

해결책

the error is because you already have the table firstTableName is already present and as you did scaffold, it will create the model, controller and view for you, so it has also create the migration for the firstTableName.

If you already have the migration in place try skipping the migration while scaffolding

rails g scaffold firstTableName --skip-migration

다른 팁

Your database should have a table named schema_migrations with only one column version. Then you should write all migration versions into that table by hands. Migrations placed in project_root/db/migrate/, and the вшпшеы in the file name is the same as version migration.

When you run migrations rails keeps track of these by matching each migration in db/migrate folder with an entry in schema_migrations table that includes timestamp for all the migrations that rails has already run for that environment in versions column.

Reason is to allow you to migrate incrementally.

Earlier versions of rails had a strong philosophy of having a down migration as well to go back in time. However, that has been left as a choice now.

Your complaint is because it was not able to create schema_migrations table. My suggestion is to allow it to do so by running rake db:migrate on a fresh db.

If you have data in the tables on this instance run it on another database and then just copy across the versions table.

http://guides.rubyonrails.org/migrations.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top