PG :: UndefinedTable:错误:关系“ ...”不存在

缺口

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

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

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

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

对于Actioncode,我具有迁移文件:

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

而动作代码模型文件包括:has_many :organizations

任何想法可能导致错误消息吗?

如果我index: true, foreign_key: true从迁移文件中删除,它将迁移而不会出现错误。当我用不正确的行替换该行时t.references :actioncode_id, index: true, foreign_key: true,它给出以下错误,最后一行(“ ids”)表明Rails某种程度上似乎与表名有关?

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")
普拉卡什·穆西(Prakash Murthy)

因此,由于CreateOrganizations迁移是在执行之前进行的,因此问题正在发生CreateActioncodes

CreateActioncodes首先运行,从而确保该action codes表存在。

迁移的运行顺序基于迁移的时间戳-如文件名所示。20141014183645_create_users.rb20141014205756_add_index_to_users_email.rb在第二个时间戳之前运行-在第一个时间戳之后运行20141014205756- 20141014183645

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

您可以手动更改文件名中的时间戳。或删除这些迁移文件,然后以正确的顺序创建它们。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PG::UndefinedTable:错误:关系“洞穴”不存在

ActiveRecord :: StatementInvalid(PG :: UndefinedTable:错误:关系“ guestbooks”不存在

PG::UndefinedTable:错误:关系“step_images”不存在

Rails - PG::UndefinedTable:错误:关系“客户”不存在

耙子流产了!PG :: UndefinedTable:错误:关系“页面”不存在

PG::UndefinedTable:错误:关系“条目”不存在

正在安装Devise:`PG :: UndefinedTable:错误:关系“用户”不存在`

Rails联接表名称与迁移或架构不匹配(PG :: UndefinedTable:错误:关系不存在)

ActiveRecord :: StatementInvalid(PG :: UndefinedTable:错误:关系“ spree_orders”不存在

PG::UndefinedTable:错误:关系“”不存在 - Amazon EC2、Postgresql 和 Rails 5

PG :: UndefinedTable-错误:关系“ active_storage_attachments”不存在

PG :: UndefinedTable:错误:关系“ active_storage_blobs”不存在

PG :: UndefinedTable:错误:表“表名”不存在

heroku postgresql数据库rails应用程序PG :: UndefinedTable:错误:关系“ comments”不存在

PG :: UndefinedTable:错误:关系“ action_text_rich_texts”不存在| Rails 6 | Rails_Admin | PostgreSQL

Rails ActiveRecord :: StatementInvalid:PG :: UndefinedTable:错误:关系“ pg_range”不存在第3行:左联接pg_range为r ON oid = rngtypid

Sidekiq PG :: UndefinedColumn:错误:不存在

Pg:错误列不存在

pg_restore错误:角色XXX不存在

PG :: UndefinedColumn:错误:关系“事件”的列“ city_id”不存在

pg_restore 错误:“关系不存在”并创建新数据库

Heroku +公寓PG ::错误:错误:函数pg_stat_statements_reset()不存在

Rails 5.1.5 PG 10.0.0获取PG :: UndefinedFunction:错误:函数gen_random_uuid()不存在

pg_restore 上的 PostgreSQL pg_upgrade 错误。“角色‘29648’不存在”

i18n-active_record PG ::错误:错误:关系“翻译”不存在(第4条)

PG :: UndefinedFunction:错误:运算符不存在:字符变化@>点

pg-promise-错误运算符不存在:bigint = bigint []

在使用pg_prepare()错误'名称'的冲突中不存在

PG :: UndefinedFunction:错误:运算符不存在:text =布尔值