首页 文章

Rails:在 生产环境 中不存在与类名称引用的关系

提问于
浏览
0

我在迁移中创建了两个引用,这些引用是对我的User表的引用的别名:

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

在我的用户模型中:

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

在我的邀请模型中:

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

一切都在开发中运行良好,但当我尝试使用Heroku heroku run rake db:migrate 进行 生产环境 迁移时,出现以下错误:

PG :: UndefinedTable:错误:关系“所有者”不存在:CREATE TABLE“邀请”(“id”串行主键,“owner_id”整数,“invitee_id”整数,“core_bot_id”整数,“email”字符变化, “token”字符变化,“created_at”时间戳NOT NULL,“updated_at”timestamp NOT NULL,CONSTRAINT“fk_rails_59e24979a9”FOREIGN KEY(“owner_id”)REFERENCES“owner”(“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“))

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

知道这里有什么问题吗?

3 回答

  • 3

    您的开发可能是一个sqLite数据库,但Heroku使用PostgreSQL,并且迁移的解释正在生成 owners 的foregn键 .

    写这样的迁移代替......

    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
    

    使用与 生产环境 实施不同的数据库产品开发风险是其中一个风险 . 迁移可能无法完全相同 . 如果计划部署到Heroku,你应该考虑在开发中使用postgreSQL .

  • 1

    我不知道如何解决你的问题 . 但是我总是使用这样的外键为Postgres DB创建迁移:

    def change
        create_table :invitations do |t|
          t.integer :owner_id
          t.integer :invitee_id
          t.references :core_bot, foreign_key: true # the associated page (core_bot_active)
          t.string :email
          t.string :token
    
          t.timestamps    
        end
        add_index :invitations, :owner_id 
        add_foreign_key :invitations, :users, column: :owner_id
        add_index :invitations, :invitee_id 
        add_foreign_key :invitations, :users, column: :invitee_id
      end
    
  • 0

    尝试从选项中删除foreign_key:true,因为我们给它一个引用,我认为我们不需要foreign_key:true选项

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

相关问题