迁移时出现以下错误

发布于 2025-01-07 13:36:37 字数 1084 浏览 1 评论 0原文

我正在开发 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

I am working on rails 2 project and am getting the following error while running rake tasks. Can someone help me out as what may be causing this.

[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2025-01-14 13:36:37

错误信息相当清楚:

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

当需要将 where_keywords.where_location_id 列创建为 varchar 时,您将其创建为 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

The error message is fairly clear:

Key columns "where_location_id" and "id" are of incompatible types: character varying and integer

You're creating the where_keywords.where_location_id column as a varchar when it needs to be an integer so that it can refer to where_locations.id in the FK. Your migration has something like this:

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

that should be more like this:

create_table :where_keywords do |t|
  #...
  t.integer :where_location_id
  #...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文