[英]PG::DependentObjectsStillExist: ERROR: cannot drop column xxx_id of xxxxx because other objects depend on it
[英]PG::DependentObjectsStillExist: ERROR: cannot drop table reports because other objects depend on it
給一些背景。 我是Ruby on Rails的新手,並且正在創建一個評論網站,用戶可以在其中報告評論。 我玩了一個“可報告的”多態關聯,然后創建了報告表。 后來我刪除了“報告”表並創建了另一個報告表。 較早的遷移不允許創建新的報告表遷移,因此我在較早遷移的遷移名稱旁邊添加了“ 1”。
20180311071357_create_reports_1.rb
class CreateReports1 < ActiveRecord::Migration[5.0]
def change
create_table :reports do |t|
t.string :reason
t.text :description
t.string :email
t.belongs_to :reportable, polymorphic: true
t.timestamps
end
end
end
快進到現在。 我有一段時間沒有對Heroku進行生產承諾了,而我只是在構建應用程序。 當我推到heroku時,出現以下錯誤-
heroku run rake db:migrate --trace
Running rake db:migrate --trace on ⬢ housereview... up, run.6689 (Free)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
(4.0ms) SELECT pg_try_advisory_lock(2386658352535867725);
ActiveRecord::SchemaMigration Load (4.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to DeleteReports1 (20180318091834)
(1.2ms) BEGIN
== 20180318091834 DeleteReports1: migrating ===================================
-- drop_table(:reports)
(2.8ms) DROP TABLE "reports"
(2.6ms) ROLLBACK
(2.3ms) SELECT pg_advisory_unlock(2386658352535867725)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DependentObjectsStillExist: ERROR: cannot drop table reports because other objects depend on it
DETAIL: constraint fk_rails_cfc8432c20 on table notes depends on table reports
HINT: Use DROP ... CASCADE to drop the dependent objects too.
: DROP TABLE "reports"
...
/app/bin/bundle:3:in `load'
/app/bin/bundle:3:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: PG::DependentObjectsStillExist: ERROR: cannot drop table reports because other objects depend on it
DETAIL: constraint fk_rails_cfc8432c20 on table notes depends on table reports
HINT: Use DROP ... CASCADE to drop the dependent objects too.
: DROP TABLE "reports"
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
.......
/app/bin/bundle:3:in `load'
/app/bin/bundle:3:in `<main>'
Caused by:
PG::DependentObjectsStillExist: ERROR: cannot drop table reports because other objects depend on it
DETAIL: constraint fk_rails_cfc8432c20 on table notes depends on table reports
HINT: Use DROP ... CASCADE to drop the dependent objects too.
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
這是我的圖式-
ActiveRecord::Schema.define(version: 20180407122611) do
create_table "locations", force: :cascade do |t|
t.string "address"
t.string "street_number"
t.string "locality"
t.string "postal_code"
t.string "country"
t.string "route"
t.string "state"
end
create_table "notes", force: :cascade do |t|
t.text "description"
t.integer "report_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["report_id"], name: "index_notes_on_report_id"
end
create_table "reports", force: :cascade do |t|
t.string "reason"
t.text "description"
t.integer "user_id"
t.integer "review_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "completed_at"
t.index ["review_id"], name: "index_reports_on_review_id"
end
create_table "reviews", force: :cascade do |t|
t.string "title"
t.string "duration"
t.text "positive"
t.text "negative"
t.integer "location_id"
t.integer "user_id"
t.integer "rating_safety"
t.integer "rating_neighbour"
t.integer "rating_owner"
t.integer "rating_school"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "rating_overall"
t.boolean "blocked"
t.index ["location_id"], name: "index_reviews_on_location_id"
end
create_table "users", 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"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "admin", default: false
t.string "first_name"
t.string "last_name"
t.string "locality"
t.string "state"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
我嘗試了(幾乎)一切來解決此問題。 到目前為止,我已經嘗試過以下操作--刪除生產應用程序,然后還原pgbackup-刪除生產應用程序,然后嘗試執行新的'git push heroku master',然后'heroku run rake db:migrate'-嘗試刪除table命令,然后再次推送代碼並耙db:migrate-嘗試“如果存在DROP TABLE,則報告級聯;” 在pgsql中
以上所有都沒有運氣。 該應用程序在開發人員(SQLite)中運行完美,但是heroku不允許我耙db:migrate。
提前致謝!
注釋表包含report_id作為外鍵,因此您無法刪除報告表。 您可以通過以下方式來執行dependent: :destroy
在report.rb文件中,更改以下行
has_many :notes
至
has_many :notes, dependent: :destroy
您是否還有創建notes
表的遷移? 我猜想該遷移中的create_table :notes
與您在db/schema.rb
看到的不完全匹配。
不管了,你的問題是notes.report_id
引用reports.id
通過在數據庫中的外鍵。 此外鍵可能是由t.references :report, foreign_key: true
在遷移中創建的,以確保引用完整性(即,數據庫將確保notes.report_id
中的任何值notes.report_id
引用了reports
表中的某些內容)。 此外鍵約束阻止您刪除reports
表,因為這會違反notes.report_id
上的約束。
我認為您實際上根本不想刪除reports
表。 相反,您希望使Heroku的reports
表與schema.rb
所說的相匹配。 遷移並不能永遠持續下去,因此繼續刪除有問題的遷移,並編寫新的遷移,使Heroku的reports
表與schema.rb
所說的保持同步。
清理完這些混亂之后,您需要做兩件事:
永遠不要重命名遷移。 刪除舊的遷移並不時添加新的。 如果您想保留一個舊的遷移,但將其名稱用作其他名稱,則在新遷移上加上一個后綴,但實際上,沒有理由永遠保留舊的遷移。
在本地開發環境中安裝PostgreSQL。 您應該始終使用相同的堆棧進行開發,測試和部署。 數據庫之間的差異如此之大,以至於ActiveRecord不會保護您免受在三種環境中使用不同數據庫的困擾,這簡直就是痛苦和痛苦的捷徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.