Migrations are pending. Unable to create more models ruby on rails - ruby-on-rails

How to create more models in ruby on rails? at first create one model like this type of command:
$ rails g model user name:string email:string
Create properly, but when I try to create second model like this type of command:
$ rails g model job job1:string job2:string
Not create any model & showing this type of error
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
How can I solve this issue? I'm using Rails 4.2.5, ruby 2.1.7p400 & mysql

As the error you are getting Migrations are pending. To resolve this issue,...
To fix this you need to follow the below step.
1: In terminal you need to Run
rake db:migrate # in order to run the all pending migration task
2: Then
rails g model job job1:string job2:string
Should work!!!
hope this solve your issue!!!

Do a rake db:migrate in your terminal every time you generate a model(s) or make some changes in your model. Only then it will be applied to your rails app.
For more on migration you can visit: http://guides.rubyonrails.org/v3.2/migrations.html

Each time you create a migration or/and add a model (or any changes to your table) you have to run rake db:migrate to apply those changes.
You can also see which migrations are pending by using rake db:migrate:status
Hope it sheds a bit of light on your issue.

Related

How to pass Migrations are pending. To resolve this issue, run in rails

I'm working on api projects in rails 4. I created all models by command rails g model myModel, some action in db/migrate has been created a file for migration db, which I do not use for this migrate.
If I run some controller, example localhost:3000/report/data
I found some error :
"Migrations are pending. To resolve this issue, run:
bin/rake db:migrate RAILS_ENV=development"
It means that, I've to run rails & migrate for every model that I created in my project.
how can I pass it for all models in rails 4? Thanks in advance.
I think you can skip migration creation using the command
rails g model User --skip-migration
Just run
rake db:reset
and then
rake db:migrate
You should run
bin/rake db:migrate RAILS_ENV="development"

Rails: How to delete a pending migration

I'm currently following the ruby on rails tutorial: http://guides.rubyonrails.org/getting_started.html.
I am trying to save data into the database. However, when I run: rails server I get the following error:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
I've looked at the other articles and when I run:
bin/rake db:migrate
I get a rake aborted!
After running:
rake db:abort_if_pending_migrations....
I see that:
You have 1 pending migration:
20150805200129 CreateDatabases
SQLite3::SQLException: table "databases" already exists:
and it just tells me to run rake db:migrate to start again.
It seems that it already exists. Is there a way to cancel the pending migration?
Sometimes, even dropping a local development database is not a good idea.
There are better ways to delete/destroy a specific migration in your Rails application.
You could use rails d migration command to destroy a particular migration:
rails d migration MigrationName
To undo the changes corresponding to a particular migration, you can use db:migrate:down method like this:
rake db:migrate:down VERSION=XXX
Sometimes, things could get more messy and in those situation another handy thing is to take a look at the schema_migrations table in your database which has all the migrations with their version saved in it.
You can delete a particular migration from this table like this:
delete from schema_migrations WHERE version = VERSION;
if you don't want that migration to be present anymore.
Your migration may have failed midway (so it created the table, but didn't finish).
You are just using development environment, so it's okay to just drop the database and rebuild it from scratch:
rake db:drop # THIS WILL DELETE YOUR DATABASE
rake db:create
rake db:migrate
If you are like me and maintain your database structure outside of Rails, you can just delete the migration file from db/migration. I got the error in the OP's question when I used the rails generate command to create a model class, forgetting that it also creates a migration file.
Do not use this method if you rely on Rails to maintain your database structure!
I keep my Rails structure file up to date by building it from the database using:
bundle exec rake db:structure:dump
I do not encourage to drop the database and start from the beginning especially when you already have the data inside the database.
My approach to this will be migrate first, then rollback. After that you can safely delete the migration file. So the procedure is as following.
rails db:migrate
rails db rollback
rm db/migrate/your_last_migration_file.rb
You can recreate database and run all migrations in your development environment with such command
rails db:migrate:reset
If you want to revert the wrong migrations, You can drop the whole db using this:
rake db:drop
Then remove the migrations file manually(This wont corrupt the db when you recreate as the Schema migrations would be dropped as well).
Then run
rake db:migrate
And if there is data to be seeded, then run this as well
rake db:setup

Creating tables in Postgres and Rails 4

I am a completely new at this; I used the psql console to create tables for the db, but I think I'm doing it wrong since when I run rake db:migrate, everything gets wiped. So my question is how do I get it to stop dropping all my tables upon a reset without typing all my tables again? I think I'm supposed to use models, is this correct?
EDIT - So I have wasted all my time making tables using the psql console?
You need to create tables "the Rails way" with migrations. You should read the link provided, but essentially you create migrations to track the changes in your database schema over time. You have migrations for creating tables, adding new columns, changing column definitions, etc. You can generate a migration to create a table from the command line like so:
rails generate model Fruit title:string amount:integer
rake db:migrate
This will not only create the table fruits with the specified name, it will also create a model Fruit with which you can query the table. You're also free to edit the migration created before running rake db:migrate.
You should create migration files to build your database. That is what you are doing when you run rake db:migrate: you are taking your migration files and applying them to your database. Look at this guide for more information about Rails migrations.
By running $ rake -T you can check every rake task. Everything is being wiped because other tasks are run along with certain rake tasks. Try
$ rake db:drop
$ rake db:create
$ rake db:migrate
Your database should be created properly, as long as you gave database creation permissions to your Postgres role.

How to resolve db:migrate error SQLite3::SQLException: no such table:

I am trying to use the social network gem inkwell.
I am following the simple instructions here .
I have created a Post, User, Category and Community model using rails generate model
I then configure the models and run
$ rake inkwell:install:migrations
$ rake db:migrate
but end up with the error
SQLite3::SQLException: no such table: posts:
There is a migrate file for this model and I always thought that once you'd created the model then following a migrate it would create a table so I'm a bit confused. Certainly when I rails console Post.all there is no table so this migration isn't happening.
As it is said in the instructions you have to create before the model for users and posts.
So if we assume you have just created the models with a generator, run the migrations for them:
$ rake db:migrate
Creating so the required tables and then:
$ rake inkwell:install:migrations
$ rake db:migrate
Did you try a rake db:reset?
Or delete the database file and:
rake db:create
rake db:migrate

Would I need to undo a "rails generate scaffold" after I undo a "db:migrate"?

I'm new to RoR and I'm following Michael Hartl's tutorial (so feel free to correct the terminology I'm using where you see it fit). In chapter 2, I created a Users table by running these lines:
$ rails generate scaffold User name:string email:string
$ bundle exec rake db:migrate
Then, I ran the code below to try to create a Microposts table (However, I misspelled Micropost without an 'r'!)...
$ rails generate scaffold Miropost content:string user_id:integer
$ bundle exec rake db:migrate
Now I want to delete the Miropost table I created. After searching in stackoverflow.com, I understand I can undo the database migration (ie., db:migrate) by running rake db:migrate:reset. My question is would I need to undo the "rails generate scaffold" too? And when do scaffolds cease to exist?
First you would need to rollback the changes from db.
Assuming that the migration generated for Miropost is the latest migration in your db.
Just run
rake db:rollback ## This will drop the table miroposts
After this destroy the existing scaffold by :
rails destroy scaffold Miropost content:string user_id:integer
Then all you need to do is to recreate the scaffold with correct spelling and run rake db:migrate

Resources