Rails 4 migration not adding fields to an existing database

I have an order database that I am trying to add two new fields to it

1) seller_id 2) buyer_id

when i run the command

rails generate migration AddFieldstoOrders buyer_id:integer seller_id:integer

it shows

 invoke  active_record
      create    db/migrate/20140414094632_add_fieldsto_orders.rb 

after running rake dd:migrate and rails s it shows

== 20140414094632 AddFieldstoOrders: migrating ================================ == 20140414094632 AddFieldstoOrders: migrated (0.0000s) =======================

it should give me the seller_id table and buyer_id table. I have double checked the orders database model via the sqlite database model viewer and both fields have not appeared

both fields have been added to my user.rb and order.rb models

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

   validates :name, presence: true

   has_many :listings, dependent: :destroy
   has_many :sales, class_name: "Order", foreign_key: "seller_id"
   has_many :purchases, class_name: "Order", foreign_key: "buyer_id"
end

order.rb

class Order < ActiveRecord::Base
    validates :address, :city, :state, presence: true

    belongs_to :listing
    belongs_to :buyer, class_name: "User"
    belongs_to :seller, class_name: "User"
end

I have successfully added a listings id to the order database using rails generate migration AddListingIdToOrders listing_id:integer but now i am unable to add the seller_id and buyer_id fields

migration file

class AddFieldstoOrders < ActiveRecord::Migration
  def change
  end
end

Answers


Without seeing the actual migration file: db/migrate/20140414094632_add_fieldsto_orders.rb, your problem is because of the lowercase t on toOrders.

The command you should have run is:

rails generate migration AddFieldsToOrders buyer_id:integer seller_id:integer

which would hvae generated a migration file db/migrate/20140414094632_add_fields_to_orders.rb (note the extra underscore)

You should do the following to fix this:

rake db:rollback
rails generate migration AddFieldsToOrders buyer_id:integer seller_id:integer
rake db:migrate

And then delete your original db/migrate/20140414094632_add_fieldsto_orders.rb . The rollback just takes the entry out the schema_migrations table in the database to make it cleaner.


It should be:

rails generate migration AddFieldsToOrders buyer_id:integer seller_id:integer

You have AddFieldstoOrders which should be AddFieldsToOrders

So now you have two options.

Option 1

  1. Rollback the previous migration ie rake db:rollback
  2. Delete it.

Run the command again ie

rails generate migration AddFieldsToOrders buyer_id:integer seller_id:integer 

Option 2

  1. Rollback the previous migration ie rake db:rollback
  2. Edit the migration ie

    add_column :orders, :buyer_id, :integer

    add_column :orders, :seller_id, :integer


Need Your Help

How to know if global module is invoked or local?

javascript node.js mocha node-modules

A node-module can be installed at local level or at global level by adding -g while doing npm install.

How get value of variable that has been optimized out?

javascript debugging firefox javascript-debugger

Some variables can be "optimized out" during Javascript execution. Thus values of such variables are not available for inspection while debugging (User documentation). Variables view shows (optimized