Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top