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

Ruby ActiveRecord Basics Query Interface Advanced Finders

sungwon choe
sungwon choe
15,044 Points

"add_index :accounts, :employees" migration returns SQL syntax error

Following along with Hampton, I tried to rake the example migration that creates an index on accounts:

class AddIndexEmployees < ActiveRecord::Migration
  def change
    add_index :accounts, :employees
  end
end

This results in the following error:

biller$ rake db:migrate
== 20140913180455 AddIndexEmployees: migrating ================================
-- add_index(:accounts, :employees)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Key column 'employees' doesn't exist in table: CREATE  INDEX `index_accounts_on_employees`  ON `accounts` (`employees`) /home/sungwon/code/treehouse/biller/db/migrate/20140913180455_add_index_employees.rb:3:in `change'
ActiveRecord::StatementInvalid: Mysql2::Error: Key column 'employees' doesn't exist in table: CREATE  INDEX `index_accounts_on_employees`  ON `accounts` (`employees`) 
/home/sungwon/code/treehouse/biller/db/migrate/20140913180455_add_index_employees.rb:3:in `change'
Mysql2::Error: Key column 'employees' doesn't exist in table
/home/sungwon/code/treehouse/biller/db/migrate/20140913180455_add_index_employees.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

The error makes sense since there is no column 'employees' (there is a column 'type' populated with 'Employee' strings for some entries), so did Hampton make some changes to the database, adding a column for 'employees' while we weren't watching?

sungwon choe
sungwon choe
15,044 Points

Oh right, he did (mention that he updated the db earlier)! Could we get the sql dump of the updated database so we can follow along with the same code and queries?

2 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

You could do this:

class AddIndexEmployees < ActiveRecord::Migration
  def change
    add_column :accounts, :employees, :integer
    add_index :accounts, :employees
  end
end

If you go to this stage (it's the last one):

http://teamtreehouse.com/library/activerecord-basics/ar-extensions/model-versioning

and click on downloads, you can get a copy of the app. Go to the db folder, and you can get the schema.rb file, which will give you a rundown of each table, with it's columns.

You can also go to the migrate folder and see each of the migrations.

Hope this helps! Happy coding =)

sungwon choe
sungwon choe
15,044 Points

Thank you, Brandon!