2

I am installing the devise gem for authentication in a Ruby on Rails application and I ran the database migration like this:

rake db:migrate

and got this error:

undefined method `reference' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x9322248>

This is a bit cryptic. Where should I go to debug this and what could the problem be?

The only non-standard thing I did was give it the table name "users" which is my table name in this previous command: rails generate devise users

Also, my routes.rb file has this new entry:

devise_for :users

Probably the issue is mis-matched columns in my database and what the auth package thinks the users table should be like. Where do I look to see what the auth package thinks the columns are like? And where do I find where the create-table command is for the users table that I have. It was made with the scaffold command originally which put a whole bunch of extra and useless things in my system.

My db/migrate/users/create_users file looks like this:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Which is basic, but my users table in the db has these columns:

+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| uid              | int(10) unsigned | NO   | PRI | 0       |       |
| name             | varchar(60)      | NO   | UNI |         |       |
| pass             | varchar(128)     | NO   |     |         |       |
| mail             | varchar(254)     | YES  | MUL |         |       |
| theme            | varchar(255)     | NO   |     |         |       |
| signature        | varchar(255)     | NO   |     |         |       |
| signature_format | varchar(255)     | YES  |     | NULL    |       |
| created          | int(11)          | NO   | MUL | 0       |       |
| access           | int(11)          | NO   | MUL | 0       |       |
| login            | int(11)          | NO   |     | 0       |       |
| status           | tinyint(4)       | NO   |     | 0       |       |
| timezone         | varchar(32)      | YES  |     | NULL    |       |
| language         | varchar(12)      | NO   |     |         |       |
| picture          | int(11)          | NO   |     | 0       |       |
| init             | varchar(254)     | YES  |     |         |       |
| data             | longblob         | YES  |     | NULL    |       |
+------------------+------------------+------+-----+---------+-------+

And I am not sure how such an inconsistency can exist after I run the migrate command. Where does it take instructions from if not the above file I posted?

Thanks!

GeekedOut
  • 16,905
  • 37
  • 107
  • 185

4 Answers4

10

I had a similar error after generating a new model:

rails generate model Status open:boolean available:integer station:reference

The problem was I was using 'reference' instead of 'references' when generating the model. This command creates the following migration:

class CreateStatuses < ActiveRecord::Migration
    def change
        create_table :statuses do |t|
            t.boolean :open
            t.integer :available
            t.reference :station
        end
    end
end

And the method 'reference' is undefined, thus the error. The migration in my case should be:

class CreateStatuses < ActiveRecord::Migration
    def change
        create_table :statuses do |t|
            t.boolean :open
            t.integer :available
            t.references :station
        end
    end
end
Cesar
  • 173
  • 2
  • 9
1

I suggest you run the db:migrate command with the --trace option:

rake db:migrate --trace

As an example, I purposely added a syntax error in my devise migration and this is a fragment of the output I got. As you can see, the --trace option should point you to the exact error (migration file + line #).

    undefined method `strin' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000106c5ea98>
    /Users/#####/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/schema_definitions.rb:326:in `method_missing'
    /Users/#####/rails/$$$$$$/db/migrate/20101031153010_devise_create_users.rb:13:in `block in up'
    /Users/#####/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/schema_statements.rb:157:in `create_table'
...

Note that your migration files are located under the db/migrate directory. So given the above error, I would need to open up the db/migrate/20101031153010_devise_create_users.rb migration file and fix the error on line 13.

mbreining
  • 7,739
  • 2
  • 33
  • 35
  • @feelnoway What I really dont understand is why there are only 4 files in my db/migrate directory, and 20 tables in my MySQL db. I used the scaffold command once, but really wondering what may have put those extra tables in there. Any thoughts on why that may have happened? – GeekedOut May 13 '11 at 18:29
  • @feelnoway I edited my question to show the migrate script and the db table that gets created. – GeekedOut May 13 '11 at 18:34
  • Without knowing the history of your environment, it's difficult to say what these tables are and where they come from. Have you considered cleaning up your database and then run your migrations again? You may want to give a try to the `rake db:migrate:reset` command but keep it mind that this will wipe out the data currently stored in your database so make you're ok with that first. If that doesn't work I would consider manually deleting all tables in your database or simply recreate a database. – mbreining May 13 '11 at 18:36
  • @feelnoway yes I want to, and will clean up the database. I am just waiting to learn the right way to do it in the ruby world. I am used to storing the separate files to recreate the tables. Is the reset command to wipe out the old tables and create new ones? – GeekedOut May 13 '11 at 18:40
  • Indeed your users table is inconsistent with your User migration. I suspect the users table might have been used in the past for another project. I suggest you simply recreate your database or wipe out all tables and then run your migrations again. Also, if you already have a User model and are trying to integrate devise, then I suggest you look at this [post](http://stackoverflow.com/questions/4175988/devise-migration-on-existing-model) which will tell you how you need to update your User migration to make it work with devise. – mbreining May 13 '11 at 18:42
  • Yes the `rake db:migrate:reset` command will wipe out your database and run the migrations again. That should put you back into a clean state. – mbreining May 13 '11 at 18:47
0

Check your migration file "migrate/20101031153010_devise_create_users.rb". You have probably made a mistake or a typo in the code.

Pavel Yermalovich
  • 1,970
  • 1
  • 15
  • 19
0

My issue was that I used :

za$ rails g scaffold team name:string team_id:integer:uniq  references:vendor

Instead of :

za$ rails g scaffold team name:string team_id:integer:uniq  vendor:references

just changed vendor:references to vendor:references

Stupid mistake, I know.

z atef
  • 7,138
  • 3
  • 55
  • 50