【问题标题】:Getting following error on migration迁移时出现以下错误
【发布时间】:2012-03-10 14:21:30
【问题描述】:

我正在处理 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

【问题讨论】:

  • 向我们展示您迁移中的代码,以及所涉及的所有表的架构。

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


【解决方案1】:

错误信息相当清楚:

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

where_keywords.where_location_id 列需要为integer 时,您将其创建为varchar,以便它可以在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

【讨论】:

  • 谢谢这解决了我的问题。我是新手,非常感谢您的指导
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
  • 2015-10-13
  • 2018-05-02
  • 2015-07-10
  • 2016-10-03
相关资源
最近更新 更多