Rails:在生产中不存在用于引用类名的关系 [英] Rails: Relation does not exist for reference with class name in production

查看:37
本文介绍了Rails:在生产中不存在用于引用类名的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在迁移中创建了两个引用,它们是对我的用户表的引用的别名:

class CreateInvitations <ActiveRecord::迁移[5.0]定义改变create_table : 邀请做 |t|t.references :owner,references: :user,foreign_key: true # 所有者t.references :invitee,references: :user,foreign_key: true # 被邀请者t.references :core_bot, foreign_key: true # 关联页面 (core_bot_active)t.string : 电子邮件t.string :tokent.时间戳结尾结尾结尾

在我的用户模型中:

 has_many :invitations, foreign_key: :owner_idhas_many :invitations, foreign_key: :invitee_id, 依赖: :destroy

在我的邀请模型中:

belongs_to :owner, class_name: :User归属地:受邀者,班级名称::用户

在开发中一切正常,但是当我尝试使用 Heroku heroku run rake db:migrate 在生产环境中迁移时,我收到以下错误:

<块引用>

PG::UndefinedTable:错误:关系所有者"不存在:创建TABLE邀请"(id"序列主键,owner_id"整数,invitee_id"整数,core_bot_id"整数,email"字符变化,令牌"字符变化,created_at"时间戳不为空,updated_at"时间戳非空,约束fk_rails_59e24979a9"FOREIGN KEY ("owner_id") REFERENCES "owners" ("id") , CONSTRAINTfk_rails_00204dc74b"外键(invitee_id")参考"被邀请者" ("id") , 约束 "fk_rails_34505bdb65" 外键(core_bot_id")参考core_bots"(id"))

我试过没有 references::user 但我得到了同样的错误.

知道这里出了什么问题吗?

解决方案

您的开发可能是一个 sqLite 数据库,但 Heroku 使用 PostgreSQL,迁移的解释是为 owners 生成一个外键

改为这样编写迁移...

class CreateInvitations <ActiveRecord::迁移[5.0]定义改变create_table : 邀请做 |t|t.references :owner, index: true # 所有者t.references :invitee, index: true # 被邀请人t.references :core_bot, foreign_key: true # 关联页面 (core_bot_active)t.string : 电子邮件t.string :tokent.时间戳结尾add_foreign_key :invitations, :users, column: :owner_idadd_foreign_key :invitations, :users, column: :invitee_id结尾结尾

这是使用与生产实施不同的数据库产品进行开发的风险之一.迁移可能不会完全相同.如果计划部署到 Heroku,您应该考虑在开发中使用 postgreSQL.

I created two references in a migration that are aliases for a reference to my User table:

class CreateInvitations < ActiveRecord::Migration[5.0]
  def change
    create_table :invitations do |t|
      t.references :owner, references: :user, foreign_key: true # the owner
      t.references :invitee, references: :user, foreign_key: true # the invitee
      t.references :core_bot, foreign_key: true # the associated page (core_bot_active)
      t.string :email
      t.string :token

      t.timestamps

    end
  end
end

In my User model:

  has_many :invitations, foreign_key: :owner_id
  has_many :invitations, foreign_key: :invitee_id, dependent: :destroy

In my Invitation model:

  belongs_to :owner, class_name: :User
  belongs_to :invitee, class_name: :User

Everything works well in development but when I try to migrate in production with Heroku heroku run rake db:migrate, I get the following error:

PG::UndefinedTable: ERROR: relation "owners" does not exist : CREATE TABLE "invitations" ("id" serial primary key, "owner_id" integer, "invitee_id" integer, "core_bot_id" integer, "email" character varying, "token" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_59e24979a9" FOREIGN KEY ("owner_id") REFERENCES "owners" ("id") , CONSTRAINT "fk_rails_00204dc74b" FOREIGN KEY ("invitee_id") REFERENCES "invitees" ("id") , CONSTRAINT "fk_rails_34505bdb65" FOREIGN KEY ("core_bot_id") REFERENCES "core_bots" ("id") )

I tried without references: :user but I get the same error.

Any idea what's wrong here?

解决方案

Your development is probably an sqLite database but Heroku uses PostgreSQL and the interpretation of the migration is generating a foregn key to owners

Write the migration like this instead...

class CreateInvitations < ActiveRecord::Migration[5.0]
  def change
    create_table :invitations do |t|
      t.references :owner, index: true # the owner
      t.references :invitee, index: true # the invitee
      t.references :core_bot, foreign_key: true # the associated page (core_bot_active)
      t.string :email
      t.string :token

      t.timestamps

    end
    add_foreign_key :invitations, :users, column: :owner_id
    add_foreign_key :invitations, :users, column: :invitee_id
  end
end

It's one of the risks with developing using a different database product than the production implementation. Migrations may not work exactly the same. If planning to deploy to Heroku you should look at using postgreSQL in development.

这篇关于Rails:在生产中不存在用于引用类名的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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