2010-07-19 26 views
3

這讓我瘋狂!這段代碼過去工作正常,但幾個星期前它停止工作,我無法解決原因。基本上一場比賽有很多補丁。在我PatchesController出現的錯誤,但其在軌控制檯這樣的重複性:ActiveRecord爲簡單的has_many關聯提供'沒有這樣的列'SQL錯誤

first_game = Game.find(:first) 
first_game.patches 

只要我使用補丁的方法,我得到這樣的:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: patches.game_true: SELECT * FROM "patches" WHERE ("patches".game_true = 1) 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:221:in `rescue in log' 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:204:in `log' 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:172:in `block in execute' 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:417:in `catch_schema_changes' 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:172:in `execute' 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:320:in `select' 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all' 
    from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all_with_query_cache' 
    from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:664:in `find_by_sql' 
    from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:1578:in `find_every' 
    from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:618:in `find' 
    from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:60:in `find' 
    from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:400:in `find_target' 
    from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:354:in `load_target' 
    from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:140:in `inspect' 
    from /usr/local/bin/irb:12:in `<main>' 

現在,SQL應該說真的'WHERE patches.game_id = 1',除非我生氣。我不知道爲什麼它會生成該SQL!

這裏的模型/ game.rb:

class Game < ActiveRecord::Base 
    has_many :patches 
end 

這裏的模型/ patches.rb:

class Patch < ActiveRecord::Base 
    belongs_to :game 
end 

而且補丁表中有 'game_id' 在餐桌上,和3項,全部爲第一場比賽。如果我得到其中一個修補程序並轉到my_patch.game,它會返回它所屬的Game對象,而不會出現任何問題。任何幫助將不勝感激!

+0

如果您手動設置關聯關鍵字,它是否工作?瞭解你不應該設置它,我只是好奇,如果我們可以找出錯誤的地方。 – jasonpgignac 2010-07-19 13:50:35

+0

Patch.find(:all,:conditions => {:game_id => 1})工作正常! first_game.patches.find(:all,:conditions => {:game_id => 1})給出相同的錯誤,並生成此SQL: SELECT * FROM「patches」WHERE(「patches」.game_true = 1 AND(「patches」,「game_id」= 1)) – TheAdmiral 2010-07-19 14:46:58

回答

0

它看起來像你正在改變主鍵的名稱出於某種原因。確保你的遊戲類沒有類似的東西:

class Game < ActiveRecord::Base 
    # Form #1 
    self.primary_key = true 

    # Form #2 
    set_primary_key true 
end 

這可以用於從「身份證」的主鍵列重命名爲任意的,而你的情況似乎是「真」。

+0

不幸的是,Game類的整個代碼都在原始文章中。 :(我通過我的項目文件進行了搜索,並且唯一一次'primary_key'和'set_primary_key'顯示在凍結軌道上。 – TheAdmiral 2010-07-19 14:57:35

+0

我創建了一個臨時Rails 2.3.5項目並生成了一個像你一樣的Game和Patch類在這裏,但它工作正常,你使用了什麼後端?這可能是一個數據庫的問題。你可以複製這與sqlite3引擎,例如? – tadman 2010-07-19 15:08:20

+1

我使用rails 2.3。8和sqlite3 ......我確實注意到ruby-sqlite3 gem的更新已經過去了一段時間,所以可能會破壞它。我會和MySQL一起去看看會發生什麼。 – TheAdmiral 2010-07-19 15:26:20

0

ummm愚蠢的問題,但沒有回溯聲稱失蹤的列被稱爲「game_true」(在補丁表上)? 我不認爲它聲稱沒有稱爲「修補程序」的列。

SQLException: no such column: patches.game_true: SELECT * FROM "patches" WHERE ("patches".game_true = 1) 

這應該改變你需要去看看。

3

我能弄清楚這個問題。它在我的遷移文件中。我在遷移文件中使用了引用助手。

def up 
create_table :reviews do |t| 
    t.integer  :potatoes 
    t.text   :comments 
    t.references :moviegoer 
    t.references :movie 
end 

我拼錯了父類的模型名稱。更正名稱,然後丟棄我的數據庫並重新創建它 rake db:drop rake db:migrate