2012-02-20 95 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

相关问题