【问题标题】:Rails migration rename index and foreign_key columnRails 迁移重命名索引和foreign_key 列
【发布时间】:2018-07-24 12:34:00
【问题描述】:

在我的 schema.rb 中,

create_table "devices", force: :cascade do |t|
    t.string "uuid"
    t.bigint "device_return_id"
    t.index ["device_return_id"], name: "index_devices_on_device_return_id"
end

create_table "device_returns", force: :cascade do |t|
  t.string "code"
  t.datetime "returned_at"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

add_foreign_key "devices", "device_returns", column: "device_return_id"

如何使用 rails g migration 将所有“device_return”更改为“exchange”?

我厌倦了 rename_table 和 rename_index。他们没有改变 t.index["device_return_id']

谢谢

【问题讨论】:

    标签: ruby-on-rails database activerecord migration schema


    【解决方案1】:

    改外键可以试试

    rails g migration ChangeForeignKeyForDevices
    

    那么您的迁移应该如下所示

    class ChangeForeignKeyForDevices < ActiveRecord::Migration
       def change
          rename_column :device_returns, :old_column_name, :new_column_name
       end
    end
    

    然后运行迁移

    rails db:migrate
    

    http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

    rename_column(table_name, column_name, new_column_name):重命名列但保留类型和内容。

    rename_index(table_name, old_name, new_name):重命名索引。

    rename_table(old_name, new_name):将名为 old_name 的表重命名为 new_name。

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 2016-02-02
      • 2013-12-14
      • 2018-06-13
      • 2018-10-03
      • 1970-01-01
      • 2015-03-01
      • 2018-08-02
      • 2016-12-02
      相关资源
      最近更新 更多