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 trialogechi1
14,455 PointsActive Record > Using Callbacks video
After running ruby rails generate migration add_money
class AddMoney < ActiveRecord::Migration
def change
add_column :account_entries, :amount, :decimal
add_column :accounts, :balance, :decimal
end
end
then, ruby rake db:migrate
, I receive the following error in the console:
== 20140303215930 CreateAccounts: migrating =================================== -- create_table(:accounts) rake aborted! StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'accounts' already exists: CREATE TABLE accounts
(id
int(11) auto_increment PRIMARY KEY, type
varchar(255), name
varchar(255), email
varchar(255), about
varchar(255), created_at
datetime, updated_at
datetime) ENGINE=InnoDB/Users/aaa/active_record/biller/db/migrate/20140303215930_create_accounts.rb:3:in change'
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'accounts' already exists: CREATE TABLE
accounts(
idint(11) auto_increment PRIMARY KEY,
typevarchar(255),
namevarchar(255),
emailvarchar(255),
aboutvarchar(255),
created_atdatetime,
updated_atdatetime) ENGINE=InnoDB
/Users/aaa/active_record/biller/db/migrate/20140303215930_create_accounts.rb:3:in
change'
Mysql2::Error: Table 'accounts' already exists
/Users/aaa/active_record/biller/db/migrate/20140303215930_create_accounts.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Since the beginning of the Query Interface video code, I've been receiving this error, that the table 'accounts' . In the schema.rb file, there is indeed an accounts table. However, it seems to work in the video, but not on my local machine. Any ideas? Thanks in advance!
2 Answers
J Scott Erickson
11,883 PointsYou could try rake db:reset - honestly it should only be running the last migration file ( or the most recent non migrated files ) in your migration folder. So it's a bit puzzling without being on your machine to fool around.
I'd go with take db:reset and then run db:migrate
Make sure you aren't adding the table in multiple migrations. And if all else fails you may need to manually remove the DB from outside of the app.
mysqladmin -u root drop your_db_name_here
It will warn you about dropping. Unless you have a bunch of seed data and so long as you have your migration files you should be okay to restore.
J Scott Erickson
11,883 PointsYou would just run rake:db create and then run your migrations again
No Problems on the help! I always enjoy thinking about this stuff.
ogechi1
14,455 Pointsogechi1
14,455 PointsThanks so much J Scott, I ran
rake db:reset
thenrake db:migrate
, and the problem was solved!ogechi1
14,455 Pointsogechi1
14,455 PointsBy the way, if I dropped the database but still have the migration files, how would I restore/recreate it? Thanks in advance