0

我已经从用户表开始我的项目,并且已经迁移到使用帐户表。在此过程中,我的 schema.rb 文件中仍有对 Users 表的旧引用,我需要将其删除并创建新引用或更新引用。

我正在尝试制定一个允许我执行此操作的迁移,但是由于没有 Users 表并且当它确实存在时,它一直抛出错误,它从来没有 account_id ,您可以在我的 schema.rb 文件中看到它的引用.

我真的只需要我的 schema.rb 文件来更新 "add_foreign_key "likes", "users", column: "account_id"

add_foreign_key "likes", "accounts", column: "account_id"

但是我发现这不可能在不产生错误的情况下进行迁移。

有什么建议么?

ActiveRecord::Schema.define(version: 2022_01_18_013836) do

  create_table "accounts", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at", precision: 6
    t.datetime "remember_created_at", precision: 6
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "username"
    t.string "first_name"
    t.string "last_name"
    t.index ["email"], name: "index_accounts_on_email", unique: true
    t.index ["reset_password_token"], name: "index_accounts_on_reset_password_token", unique: true
  end

  create_table "likes", force: :cascade do |t|
    t.integer "account_id", null: false
    t.integer "product_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["account_id"], name: "index_likes_on_account_id"
    t.index ["product_id"], name: "index_likes_on_product_id"
  end

  create_table "products", force: :cascade do |t|
    t.string "product_name"
    t.string "product_category"
    t.string "product_type"
    t.string "product_image"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.text "product_description"
    t.string "product_country"
  end

  add_foreign_key "likes", "products"
  add_foreign_key "likes", "users", column: "account_id"
end
4

1 回答 1

1

解决此问题的最佳方法是创建一个迁移,仅使用 account_id 添加用户,然后删除外键,然后再次删除用户表。应该在 1 次迁移中可行,但是我按照以下方式进行。

我最终创建了一个只有 account_id:integer 的新用户表创建了一个迁移到 remove_foreign_key 然后创建了一个迁移,然后再次删除该用户表。

架构文件现在看起来是正确的,我有所有的迁移来跟踪我的更改。

于 2022-01-18T23:00:45.353 回答