I was working like a fiend on a project, and forgot to add :id => false to a join table migration. For completeness, here’s the migration as it is supposed to look:

class CreateEmployeesRoles < ActiveRecord::Migration
  def self.up
    create_table :employees_roles, :id => false do |t|
      t.references :employee
      t.references :role
    end
  end

  def self.down
    drop_table :employees_roles
  end
end

As I said, I forgot the :id => false, so it put a primary key on the table, which causes rails to error out with:

Primary key is not allowed in a has_and_belongs_to_many join table (employees_roles).

K, actually, the id column doesn’t really affect it, but heh it’s not needed, so let’s get rid of it.

Unfortunately, you can’t use rake db:rollback, it gives you the same error. Understandable, but technically, that is kind of a bug ala the random feature. If you make a mistake in your migration, ideal you should be able to rollback irregardless of table specific errors (That’s why you’re rolling back in the first place.)

In order to fix this, you’ll have to do it via MySQL:

$ mysql -u username -p yourapp_development
mysql> alter table your_table drop column id;

After that you can run:

$ rake db:rollback
$ rake db:migrate

And then you should be nice and fixed up.