Rails:PG::UndefinedTable:错误:关系“...";不存在 [英] Rails: PG::UndefinedTable: ERROR: relation "..." does not exist

查看:49
本文介绍了Rails:PG::UndefinedTable:错误:关系“...";不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在迁移时,我收到以下错误消息:

On migration I get the following error message:

PG::UndefinedTable: ERROR:  relation "actioncodes" does not exist
: ALTER TABLE "organizations" ADD CONSTRAINT "fk_rails_4ecaa2493e"
FOREIGN KEY ("actioncode_id")
  REFERENCES "actioncodes" ("id")

我有以下组织的迁移文件:

I have the following migration file for Organizations:

class CreateOrganizations < ActiveRecord::Migration
  def change
    create_table :organizations do |t|
      t.string     :name,         null: false,    limit: 40
      t.references :actioncode,   index: true,    foreign_key: true
      t.boolean    :activated
      t.datetime   :activated_at

      t.timestamps null: false
    end
  end
end

对于 Actioncodes,我有迁移文件:

And for Actioncodes I have the migration file:

class CreateActioncodes < ActiveRecord::Migration
  def change
    create_table :actioncodes do |t|
      t.string  :code,          null: false,  limit: 20
      t.string  :description,                 limit: 255

      t.timestamps null: false
    end
  end
end
class AddIndexToActioncodesCode < ActiveRecord::Migration
  def change
    add_index :actioncodes, :code,  unique: true
  end
end

组织模型文件包括:belongs_to :actioncode.

虽然 actioncodes 模型文件包括:has_many :organizations.

While the actioncodes model file includes: has_many :organizations.

知道是什么导致了错误消息吗?

如果我从迁移文件中删除 index: true, foreign_key: true,它迁移时不会出错.当我用不正确的行 t.references :actioncode_id, index: true, foreign_key: true 替换该行时,它给出了下面的错误,其中最后一行(ids")暗示 Rails 以某种方式似乎表名有问题?

If I remove index: true, foreign_key: true from the migration file, it migrates without errors. And when I replace that line with the incorrect line t.references :actioncode_id, index: true, foreign_key: true, it gives the error below, where the last line ("ids") suggests Rails somehow seems to have problem with the name of the table?

PG::UndefinedTable: ERROR:  relation "actioncode_ids" does not exist
: ALTER TABLE "organizations" ADD CONSTRAINT "fk_rails_604f95d1a1"
FOREIGN KEY ("actioncode_id_id")
  REFERENCES "actioncode_ids" ("id")

推荐答案

所以问题的发生是因为 CreateOrganizations 迁移在 CreateActioncodes 执行之前运行.

So the issue is happening because CreateOrganizations migration is being run before CreateActioncodes is executed.

CreateActioncodes 将首先运行,从而确保 action 代码 表存在.

CreateActioncodes is to be run first thereby ensuring that the action codes table exists.

迁移运行的顺序基于迁移的时间戳 - 如文件名称所示.20141014183645_create_users.rb 将在 20141014205756_add_index_to_users_email.rb 之前运行,作为第二个的时间戳 - 20141014205756 第一个 - 20141014183645.

The order in which migrations are run is based on the time stamp of the migration - as indicated in the name of the file. 20141014183645_create_users.rb will run before 20141014205756_add_index_to_users_email.rb as the timestamp of the second one - 20141014205756 is after that of the first one - 20141014183645.

确保 CreateOrganizations 迁移的时间戳在 CreateActioncodes 迁移之后.

Make sure the time-stamps of the CreateOrganizations migration is after that of CreateActioncodes migration.

您也可以手动更改文件名中的时间戳.或者删除这些迁移文件,并按照正确的顺序创建它们.

Either you could manually change the timestamp in the file names. Or delete these migration files, and create them in the correct order.

这篇关于Rails:PG::UndefinedTable:错误:关系“...";不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆