Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Christ khodabakhshi
10,916 Pointsrake db:migrate runs all previous migrations
when i rub rake db:migrate it stats to run all my migrations, what is wrong here?
class NewOne < ActiveRecord::Migration
def change
add_column :accounts, :state, :string
add_column :accounts, :city, :string
add_column :accounts, :zipcode, :integer
end
end
I even call it with version number but it is still the same
$ rake db:migrate VERSION=20141106051252
The below files are all the migrates
20140303193914_create_customers.rb
class CreateCustomers < ActiveRecord::Migration
def change
create_table :customers do |t|
t.string :name, :about
t.integer :balance
t.timestamps
end
end
end
20140303194704_add_email_address.rb
class AddEmailAddress < ActiveRecord::Migration
def up
add_column :customers, :email, :string
end
def down
remove_column :customers, :email
end
end
20140303195119_create_time_entries.rb
class CreateTimeEntries < ActiveRecord::Migration
def change
create_table :time_entries do |t|
t.float :time
t.belongs_to :customer
t.belongs_to :employee
t.timestamps
end
end
end
20140303195443_create_employees.rb
class CreateEmployees < ActiveRecord::Migration
def change
create_table :employees do |t|
t.string :name, :email
t.timestamps
end
end
end
20140303215930_create_accounts.rb
class CreateAccounts < ActiveRecord::Migration
def change
create_table :accounts do |t|
t.string :type
t.string :name, :email, :about
t.timestamps
end
end
end
20140303220204_remove_old_tables.rb
class RemoveOldTables < ActiveRecord::Migration
def up
Customer.all.each do |c|
Account.create(name: c.name, about: c.about)
end
Employee.all.each do |e|
Account.create(name: e.name, email: e.email)
end
drop_table :customers
drop_table :employees
end
def down
end
end
20140303221421_account_entries.rb
class AccountEntries < ActiveRecord::Migration
def change
add_column :time_entries, :account_id, :integer
rename_table :time_entries, :account_entries
end
end
20140303223223_create_projects.rb
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :name
t.integer :customer_id
t.timestamps
end
end
end
20140303223304_employees_projects.rb
class EmployeesProjects < ActiveRecord::Migration
def change
create_table :employees_projects do |t|
t.belongs_to :employee, :project
t.timestamps
end
end
end
20140303224605_omg_i_forgot_the_type_field.rb
class OmgIForgotTheTypeField < ActiveRecord::Migration
def change
add_column :account_entries, :type, :string
end
end
20141106042647_add_columns_to_account.rb
class AddColumnsToAccount < ActiveRecord::Migration
def change
add_column :accounts, :state, :string
add_column :accounts, :city, :string
add_column :accounts, :zipcode, :integer
end
end
3 Answers
Maciej Czuchnowski
36,441 PointsOK, now please run this command:
rake db:migrate:status
and paste the output.
Also, paste your schema.rb file from the project.
Christ khodabakhshi
10,916 Pointschristkhodabakhshi@Christs-Air ~/projects/biller (master)$ rake db:migrate:status
database: biller_development
Status Migration ID Migration Name
--------------------------------------------------
up 20140303193914 Create customers
up 20140303194704 Add email address
up 20140303195119 Create time entries
up 20140303195443 Create employees
up 20140303215930 Create accounts
up 20140303220204 Remove old tables
down 20140303221421 Account entries
down 20140303223223 Create projects
down 20140303223304 Employees projects
down 20140303224605 Omg i forgot the type field
up 20141103030808 ********** NO FILE **********
up 20141103040322 ********** NO FILE **********
up 20141103041758 ********** NO FILE **********
up 20141103042828 ********** NO FILE **********
up 20141104014619 ********** NO FILE **********
up 20141104015930 ********** NO FILE **********
up 20141104024311 ********** NO FILE **********
up 20141104040751 ********** NO FILE **********
up 20141104041202 ********** NO FILE **********
up 20141104220952 ********** NO FILE **********
down 20141106042647 Add columns to account
up 20141106051252 ********** NO FILE **********
schema.rb
ActiveRecord::Schema.define(version: 20141106051252) do
create_table "account_entries", force: true do |t|
t.float "time"
t.integer "customer_id"
t.integer "employee_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "account_id"
t.string "type"
end
create_table "accounts", force: true do |t|
t.string "type"
t.string "name"
t.string "email"
t.string "about"
t.datetime "created_at"
t.datetime "updated_at"
t.string "state"
t.string "city"
t.integer "zipcode"
end
create_table "employees_projects", force: true do |t|
t.integer "employee_id"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects", force: true do |t|
t.string "name"
t.integer "customer_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "time_entries", force: true do |t|
t.float "time"
t.integer "customer_id"
t.integer "employee_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "account_id"
end
end
Maciej Czuchnowski
36,441 PointsWow, looks like you deleted some of the migration files at some point...that's a big no-no :). But there's still hope. If you don't have any important data in this database, please try this:
rake db:reset
This should destroy and recreate the whole database from scratch and apply all migration files it finds in the folder. I'm not sure how it will behave in this situation (perhaps it will still look for files that aren't there), but perhaps it will work.
Maciej Czuchnowski
36,441 Pointsrake:db migrate always runs ALL your migrations in the migrate folder, in the order of their creation. You can then undo them one at a time using rake db:rollback. You can see the status of your migrations using rake db:migrate:status. What do you want to do exactly? If you want to remove some columns that you created in a migration, you should create a new migration that deletes those columns and gets applied last.
Christ khodabakhshi
10,916 PointsI am trying to add columns to my database with the migration which I mentioned in the question, but I get this error
== AccountEntries: migrating ================================================= -- add_column(:time_entries, :account_id, :integer) rake aborted! StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Duplicate column name 'account_id': ALTER TABLE time_entries ADD account_id int(11)/Users/christkhodabakhshi/projects/biller/db/migrate/20140303221421_account_entries.rb:3:in change'
ActiveRecord::StatementInvalid: Mysql2::Error: Duplicate column name 'account_id': ALTER TABLEtime_entriesADDaccount_idint(11)
/Users/christkhodabakhshi/projects/biller/db/migrate/20140303221421_account_entries.rb:3:inchange'
Mysql2::Error: Duplicate column name 'account_id'
/Users/christkhodabakhshi/projects/biller/db/migrate/20140303221421_account_entries.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Maciej Czuchnowski
36,441 PointsLooks like you have a duplicate column in this file: 20140303221421_account_entries.rb. Look into it. And post all your migrations here if you can (using Markdown).
Christ khodabakhshi
10,916 PointsMaciej Czuchnowski, thank you for your help. I really appreciate the help and the time that you spent to help me :D I hope that one day I can do the same for others.
Maciej Czuchnowski
36,441 PointsSo...did it help? :)
Christ khodabakhshi
10,916 PointsYes, it did. Should I use rake db:rollback for deleting a migration ?
Maciej Czuchnowski
36,441 PointsNo, you should create a new migration that removes the columns you no longer need. That's the Rails way :). Rollback only undoes selected migrations, but will apply them again when you run db:migrate, and you will run it sooner or later.
Christ khodabakhshi
10,916 PointsChrist khodabakhshi
10,916 PointsI solved it with
rake db:migrate:redo VERSION=20141106042647
but what can be permanent solution?