【问题标题】:Rails 5 Migration - Missing referenceRails 5 迁移 - 缺少参考
【发布时间】:2017-10-14 16:49:12
【问题描述】:

我不确定我缺少什么 - 我最近将我的 Product 类别从静态枚举移到了表格,所以我需要修改参考。

我将结合我构建 Tasks 表的大量迁移,我需要引用 Product

class CreateTasks < ActiveRecord::Migration[5.1]
  def change
    create_table :tasks do |t|
      t.string :task_name
      t.text :comment
      t.datetime :task_start_date
      t.datetime :task_end_date
      t.references :project

      t.timestamps
    end
  end
end

class AddDocumentsToTasks < ActiveRecord::Migration[5.1]
  def change
    add_reference :tasks, :document, foreign_key: true
    add_reference :tasks, :product, foreign_key: true
  end
end


class AddClientIdToTasks < ActiveRecord::Migration[5.1]
  def change
    add_foreign_key :tasks, :client, foreign_key: true
  end
end

现在是新架构(简化)

 create_table "products", force: :cascade do |t|
    t.string "product_name"
    t.text "product_description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "client_id"
  end

  create_table "tasks", force: :cascade do |t|
    t.string "task_name"
    t.text "comment"
    t.datetime "task_start_date"
    t.datetime "task_end_date"
    t.bigint "project_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "product"
    t.bigint "document_id"
    t.boolean "completed"
    t.integer "client_id"
    t.index ["document_id"], name: "index_tasks_on_document_id"
    t.index ["project_id"], name: "index_tasks_on_project_id"
  end

我实际上不知道任务文件夹中t.integer "product" 的来源。我都看过了。

由于以下警告,它目前正在中断所有集成/播种: ActiveRecord::AssociationTypeMismatch: Product(#69974683871240) expected, got 1 which is an instance of Integer(#13017840)

我认为这是我遗漏的非常简单的东西,但由于它是非常冗余的代码,我不太清楚为什么它适用于文档/项目,但不适用于产品。

以防万一: 产品迁移

class CreateProducts < ActiveRecord::Migration[5.1]
  def change
    create_table :products do |t|
      t.string :product_name
      t.text :product_description
      t.references :client
      t.references :task

      t.timestamps
    end
  end

结束**

更新

在我完全理解为什么之前不回答这个问题,但似乎我误解了 rails db:reset 所做的事情。一旦我逐步删除/创建/迁移/播种,整个数据库结构就会启动,新的架构就会发生。

似乎 db:reset 只是使用了我的 Schema.rb 文件中的逻辑。

【问题讨论】:

  • 你的问题还在吗?

标签: ruby-on-rails activerecord


【解决方案1】:

您的schema.rb 用作迁移的缓存。因此,如果您更改已迁移的迁移文件,则不会出现修改。您必须删除您的 schema.rb 内容,然后重置您的数据库。

【讨论】:

    猜你喜欢
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2011-08-21
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多