2011-05-13 125 views
2

我在Ruby on Rails應用程序安裝色器件寶石進行身份驗證,我跑了數據庫遷移這樣的:錯誤運行Ruby on Rails的數據庫遷移

rake db:migrate 

,並得到這個錯誤:

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

這有點神祕。我應該在哪裏進行調試,問題是什麼?

我做的唯一非標準件事是給它的表名「用戶」,這是在此之前的命令我的表名:軌生成設計用戶

而且,我的routes.rb文件有這個新條目:

devise_for :users 

可能是問題是我的數據庫中的不匹配的列以及認證包認爲用戶表應該是什麼樣的問題。我在哪裏看看auth軟件包認爲這些列是什麼樣的?我在哪裏可以找到create-table命令用於我擁有的用戶表的位置。它最初是用腳手架命令製作的,它在我的系統中放置了大量額外和無用的東西。

我的DB /遷移/用戶/ create_users文件看起來像這樣:

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

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :users 
    end 
end 

這是基本的,但我的用戶在db表中有這些列:

+------------------+------------------+------+-----+---------+-------+ 
| 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 |  | 
+------------------+------------------+------+-----+---------+-------+ 

而且我不確定在運行migrate命令後如何存在這種不一致性。如果不是我發佈的上述文件,它從哪裏接受指示?

謝謝!

+0

您可以粘貼未遷移的遷移文件(或文件)的內容嗎? – twmills 2011-05-13 18:18:05

+0

@twmills實際上,這是問題的一部分 - 我不完全確定如何得到這些文件大聲笑......他們通常會在哪裏找到? – GeekedOut 2011-05-13 18:22:30

+0

您之前是否運行過遷移? – eveevans 2011-05-13 18:42:26

回答

1

我建議你運行分貝:遷移與--trace選項命令:

rake db:migrate --trace 

舉個例子,我特意在我的色器件移民增添了語法錯誤,這是我輸出的片段得到。正如你所看到的那樣,--trace選項應該指向你確切的錯誤(遷移文件+行號)。

undefined method `strin' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000106c5ea98> 
    /Users/#####/.rvm/gems/[email protected]/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/[email protected]/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/schema_statements.rb:157:in `create_table' 
... 

注意遷移文件所在的目錄db/migrate下。因此,鑑於上面的錯誤,我需要去開拓DB /遷移/ 20101031153010_devise_create_users.rb遷移文件,並修復錯誤第13行

+0

@feelnoway我真的不明白爲什麼我的db/migrate目錄中只有4個文件,而我的MySQL數據庫中只有20個表。我曾經使用scaffold命令,但真正想知道可能會把那些額外的表放在那裏。有關爲什麼可能發生的任何想法? – GeekedOut 2011-05-13 18:29:33

+0

@feelnoway我編輯了我的問題,以顯示遷移腳本和創建的數據庫表。 – GeekedOut 2011-05-13 18:34:09

+0

不知道你的環境的歷史,很難說這些表是什麼,它們來自哪裏。你有沒有考慮清理你的數據庫,然後再次運行你的遷移?您可能想嘗試'rake db:migrate:reset'命令,但請記住這會消除當前存儲在數據庫中的數據,因此您可以先確認數據。如果這不起作用,我會考慮手動刪除數據庫中的所有表或者重新創建一個數據庫。 – mbreining 2011-05-13 18:36:13

10

我產生一種新的模式後也有類似的錯誤:

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

問題是我在生成模型時使用'reference'而不是'references'。此命令創建以下遷移:

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

並且方法'reference'未定義,因此錯誤。在我的情況,遷移應該是:

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

檢查遷移文件「遷移/ 20101031153010_devise_create_users.rb」。 您可能在代碼中犯了錯誤或錯字。

0

我的問題是,我用:

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

相反的:

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

只是改變vendor:references to vendor:references

愚蠢的錯誤,我知道了。