2012-02-20 217 views
1

我正在使用rails 2項目,並在運行rake任務時收到以下錯誤。有人可以幫我解決這個問題。遷移時出現跟隨錯誤

[[email protected] webapp]# rake db:migrate 
(in /root/public/webapp) 
== CreateWhereKeywords: migrating ============================================ 
-- create_table(:where_keywords) 
NOTICE: CREATE TABLE will create implicit sequence "where_keywords_id_seq" for serial column "where_keywords.id" 
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "where_keywords_pkey" for table "where_keywords" 
    -> 0.0838s 
-- execute("alter table where_keywords add constraint where_keyword foreign key (where_location_id) references \n  where_locations(id) on delete cascade") 
rake aborted! 
An error has occurred, this and all later migrations canceled: 

PGError: ERROR: foreign key constraint "where_keyword" cannot be implemented 
DETAIL: Key columns "where_location_id" and "id" are of incompatible types: character varying and integer. 
: alter table where_keywords add constraint where_keyword foreign key (where_location_id) references 
     where_locations(id) on delete cascade 
+0

顯示我們在遷移的代碼,以及所涉及的所有表的架構。 – 2012-02-20 07:59:22

回答

3

的錯誤信息是相當明確的:

鍵列 「where_location_id」 和 「ID」 不兼容類型:字符改變和整數

要創建的where_keywords.where_location_id列作爲varchar,當它需要爲integer時,以便它可以參考FK中的where_locations.id。您的遷移有這樣的事情:

create_table :where_keywords do |t| 
    #... 
    t.string :where_location_id 
    #... 
end 

,應該是更多這樣的:

create_table :where_keywords do |t| 
    #... 
    t.integer :where_location_id 
    #... 
end 
+0

謝謝你解決了我的問題。我是新手,非常感謝你的指導 – 2012-02-20 08:38:28

相關問題