我不小心添加了索引:
class AddRandomIndices < ActiveRecord::Migration[5.0]
def change
add_index :charges, :pp_charge_id
add_index :new_refunds, :pp_refund_id
add_index :inventory_items, :product_id, unique: true
add_index :prestock_items, :product_id, unique: true
end
end
這是架構中的結果:
create_table "charges", force: :cascade do |t|
...
t.index ["pp_charge_id"], name: "index_charges_on_pp_charge_id", using: :btree
end
create_table "new_refunds", force: :cascade do |t|
...
t.index ["pp_refund_id"], name: "index_new_refunds_on_pp_refund_id", using: :btree
end
create_table "prestock_items", force: :cascade do |t|
...
t.index ["product_id"], name: "index_prestock_items_on_product_id", unique: true, using: :btree
end
create_table "inventory_items", force: :cascade do |t|
...
t.index ["product_id"], name: "index_inventory_items_on_product_id", unique: true, using: :btree
end
這是我嘗試運行的洗掉檔案:
class RemoveIndex < ActiveRecord::Migration[5.0]
def change
remove_index :charges, :pp_charge_id
remove_index :new_refunds, :pp_refund_id
remove_index :inventory_items, :product_id
remove_index :prestock_items, :product_id
end
end
這是錯誤
> rake db:migrate
== 20220515045335 RemoveIndex: migrating ======================================
-- remove_index(:charges, :pp_charge_id)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
No indexes found on charges with the options provided.
在另一個 SO 答案中:如何洗掉 rails 中的索引,有很多建議的選項......它們都沒有奏效。
在這里,我嘗試運行一個明確指定列和名稱的檔案
class RemoveIndex < ActiveRecord::Migration[5.0]
def change
remove_index :charges, column: :pp_charge_id, name: :index_charges_on_pp_charge_id
remove_index :new_refunds, column: :pp_refund_id, name: :index_new_refunds_on_pp_refund_id
remove_index :inventory_items, column: :product_id, name: :index_prestock_items_on_product_id
remove_index :prestock_items, column: :product_id, name: :index_inventory_items_on_product_id
end
end
同樣的錯誤:
> rake db:migrate
== 20220515045335 RemoveIndex: migrating ======================================
-- remove_index(:charges, {:column=>:pp_charge_id, :name=>:index_charges_on_pp_charge_id})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
No indexes found on charges with the options provided.
然后我嘗試只用名稱運行檔案,沒有列
class RemoveIndex < ActiveRecord::Migration[5.0]
def change
remove_index :charges, name: :index_charges_on_pp_charge_id
remove_index :new_refunds, name: :index_new_refunds_on_pp_refund_id
remove_index :inventory_items, name: :index_prestock_items_on_product_id
remove_index :prestock_items, name: :index_inventory_items_on_product_id
end
end
不同的錯誤:
> rake db:migrate
== 20220515045335 RemoveIndex: migrating ======================================
-- remove_index(:charges, {:name=>:index_charges_on_pp_charge_id})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedObject: ERROR: index "index_charges_on_pp_charge_id" does not exist
: DROP INDEX "index_charges_on_pp_charge_id"
我也嘗試過傳遞字串,并且不會繼續復制/粘貼......
怎么了??!
uj5u.com熱心網友回復:
您使用的語法對于洗掉索引是正確的,不確定會發生什么,但最好檢查索引是否確實存在于資料庫中或不使用 SQL 查詢,您可以在此處找到它:如何列出創建的索引對于 postgres 中的表
架構會自動與資料庫同步,但在您的情況下,您有一個引發錯誤的遷移,這就是它沒有被更新的原因,因此您可以嘗試洗掉引發錯誤然后運行的遷移rails db:migrate,如果索引沒有存在于資料庫中,它們也將從模式中洗掉。
通常,如果您錯誤地添加了一些遷移并且尚未將其推送到版本控制或尚未與其他任何人共享,那么您可以運行rails db:rollback如果它是最后一次遷移或運行rails db:migrate:status以獲取您想要回滾的遷移的版本號和然后運行rails db:migrate:down VERSION=20220515045335(僅當遷移狀態為 up 時才會運行)這將回滾遷移,然后您可以安全地洗掉檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/475643.html
上一篇:法拉第軌道從它開始
下一篇:Rails匯總記錄中的所有價格
