在迁移时获得以下错误

时间:2012-02-20 07:57:28

标签: ruby-on-rails ruby postgresql rake rails-migrations

我正在使用rails 2项目,并在运行rake任务时遇到以下错误。有人可以帮助我解决可能导致这种情况的原因。

[root@localhost 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

1 个答案:

答案 0 :(得分:3)

错误信息非常清楚:

  

键列“where_location_id”和“id”属于不兼容的类型:字符变化和整数

where_keywords.where_location_id列需要varchar时,您将integer列创建为where_locations.id列,以便它可以引用FK中的create_table :where_keywords do |t| #... t.string :where_location_id #... end 。您的迁移有类似的内容:

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

应该更像这样:

{{1}}