部署到heroku

时间:2018-05-01 18:34:48

标签: ruby-on-rails postgresql heroku activerecord database-migration

我正在尝试将对代码进行的重大更改部署到heroku中。

当我运行heroku运行rails db:migrate时出现以下错误:

I, [2018-05-01T18:07:40.587276 #4]  INFO -- : Migrating to AddShopToListing (20180415173504)
D, [2018-05-01T18:07:40.592452 #4] DEBUG -- :    (1.1ms)  BEGIN
== 20180415173504 AddShopToListing: migrating =================================
-- add_reference(:listings, :shop, {:references=>:shops, :index=>true})
D, [2018-05-01T18:07:40.597146 #4] DEBUG -- :    (2.7ms)  ALTER TABLE "listings" ADD "shop_id" bigint
D, [2018-05-01T18:07:40.690959 #4] DEBUG -- :    (87.4ms)  CREATE  INDEX  "index_listings_on_shop_id" ON "listings"  ("shop_id")
   -> 0.0983s
-- add_foreign_key(:listings, :shop, {:column=>:shop_id})
D, [2018-05-01T18:07:40.699348 #4] DEBUG -- :    (6.9ms)  ALTER TABLE "listings" ADD CONSTRAINT "fk_rails_72ed4e1344"
FOREIGN KEY ("shop_id")
  REFERENCES "shop" ("id")

D, [2018-05-01T18:07:40.704937 #4] DEBUG -- :    (5.3ms)  ROLLBACK
D, [2018-05-01T18:07:40.709352 #4] DEBUG -- :    (4.1ms)  SELECT pg_advisory_unlock(391033951906602690)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "shop" does not exist
: ALTER TABLE "listings" ADD CONSTRAINT "fk_rails_72ed4e1344"
FOREIGN KEY ("shop_id")
  REFERENCES "shop" ("id")

以下是违规的迁移文件:

class AddShopToListing < ActiveRecord::Migration[5.1]
  def change
    add_reference :listings, :shop, references: :shops, index: true
    add_foreign_key :listings, :shop, column: :shop_id
  end
end

我找不到导致此问题的原因 - 我唯一看到的是在我的heroku架构中我有以下几行:

  add_foreign_key "sellers", "users"
  add_foreign_key "shops", "listings"
  add_foreign_key "shops", "sellers"
  add_foreign_key "shops", "users"

当我在本地架构上时,我没有。

这是本地架构:

ActiveRecord::Schema.define(version: 20180501104731) do

  create_table "letsencrypt_plugin_challenges", force: :cascade do |t|
    t.text "response"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "letsencrypt_plugin_settings", force: :cascade do |t|
    t.text "private_key"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "listings", force: :cascade do |t|
    t.integer "listing_id"
    t.string "state"
    t.integer "user_id"
    t.integer "category_id"
    t.string "title"
    t.float "original_creation_tsz"
    t.float "ending_tsz"
    t.float "last_modified_tsz"
    t.float "creation_tsz"
    t.string "price"
    t.string "currency_code"
    t.integer "quantity"
    t.string "tag_1"
    t.string "tag_2"
    t.string "tag_3"
    t.string "tag_4"
    t.string "tag_5"
    t.string "tag_6"
    t.string "tag_7"
    t.string "tag_8"
    t.string "tag_9"
    t.string "tag_10"
    t.string "tag_11"
    t.string "tag_12"
    t.string "tag_13"
    t.integer "category_path_ids"
    t.float "state_tsz"
    t.string "url"
    t.integer "views"
    t.integer "num_favorers"
    t.boolean "is_supply"
    t.boolean "is_private"
    t.string "recipient"
    t.string "occasion"
    t.boolean "is_digital"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "tags", default: "--- []\n"
    t.string "img"
    t.integer "shop_id"
    t.index ["listing_id"], name: "index_listings_on_listing_id", unique: true
    t.index ["shop_id"], name: "index_listings_on_shop_id"
    t.index ["tag_1"], name: "index_listings_on_tag_1"
    t.index ["tag_10"], name: "index_listings_on_tag_10"
    t.index ["tag_11"], name: "index_listings_on_tag_11"
    t.index ["tag_12"], name: "index_listings_on_tag_12"
    t.index ["tag_13"], name: "index_listings_on_tag_13"
    t.index ["tag_2"], name: "index_listings_on_tag_2"
    t.index ["tag_3"], name: "index_listings_on_tag_3"
    t.index ["tag_4"], name: "index_listings_on_tag_4"
    t.index ["tag_5"], name: "index_listings_on_tag_5"
    t.index ["tag_6"], name: "index_listings_on_tag_6"
    t.index ["tag_7"], name: "index_listings_on_tag_7"
    t.index ["tag_8"], name: "index_listings_on_tag_8"
    t.index ["tag_9"], name: "index_listings_on_tag_9"
  end

  create_table "searches", force: :cascade do |t|
    t.string "term"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "searches_listings", force: :cascade do |t|
    t.integer "search_id"
    t.integer "listing_id"
    t.index ["listing_id"], name: "index_searches_listings_on_listing_id"
    t.index ["search_id"], name: "index_searches_listings_on_search_id"
  end

  create_table "sellers", force: :cascade do |t|
    t.string "name"
    t.integer "seller_id"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_sellers_on_user_id"
  end

  create_table "shops", force: :cascade do |t|
    t.integer "shop_id"
    t.string "shop_name"
    t.integer "user_id"
    t.float "creation_tsz"
    t.string "title"
    t.string "announcement"
    t.boolean "is_vacation"
    t.float "last_updated_tsz"
    t.integer "listing_active_count"
    t.integer "digital_listing_count"
    t.string "login_name"
    t.string "url"
    t.string "image_url_760x100"
    t.integer "num_favorers"
    t.string "icon_url_fullxfull"
    t.boolean "is_using_structured_policies"
    t.integer "listing_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "seller_id"
    t.index ["listing_id"], name: "index_shops_on_listing_id"
    t.index ["seller_id"], name: "index_shops_on_seller_id"
    t.index ["user_id"], name: "index_shops_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "password_digest"
    t.string "remember_digest"
    t.boolean "admin", default: false
    t.string "activation_digest"
    t.boolean "activated", default: false
    t.datetime "activated_at"
    t.string "reset_digest"
    t.datetime "reset_sent_at"
    t.string "request_token"
    t.string "request_secret"
    t.string "oauth_verifier"
    t.string "oauth_token"
    t.string "login_name"
    t.integer "user_id"
    t.boolean "accept_terms"
    t.boolean "accept_privacy"
    t.index ["email"], name: "index_users_on_email", unique: true
  end

end

这是heroku架构:

  enable_extension "plpgsql"

  create_table "letsencrypt_plugin_challenges", id: :serial, force: :cascade do |t|
    t.text "response"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "letsencrypt_plugin_settings", id: :serial, force: :cascade do |t|
    t.text "private_key"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "listings", force: :cascade do |t|
    t.integer "listing_id"
    t.string "state"
    t.integer "user_id"
    t.integer "category_id"
    t.string "title"
    t.float "original_creation_tsz"
    t.float "ending_tsz"
    t.float "last_modified_tsz"
    t.float "creation_tsz"
    t.string "price"
    t.string "currency_code"
    t.integer "quantity"
    t.string "tag_1"
    t.string "tag_2"
    t.string "tag_3"
    t.string "tag_4"
    t.string "tag_5"
    t.string "tag_6"
    t.string "tag_7"
    t.string "tag_8"
    t.string "tag_9"
    t.string "tag_10"
    t.string "tag_11"
    t.string "tag_12"
    t.string "tag_13"
    t.integer "category_path_ids"
    t.float "state_tsz"
    t.string "url"
    t.integer "views"
    t.integer "num_favorers"
    t.boolean "is_supply"
    t.boolean "is_private"
    t.string "recipient"
    t.string "occasion"
    t.boolean "is_digital"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "tags", default: "--- []\n"
    t.string "img"
    t.index ["listing_id"], name: "index_listings_on_listing_id", unique: true
    t.index ["tag_1"], name: "index_listings_on_tag_1"
    t.index ["tag_10"], name: "index_listings_on_tag_10"
    t.index ["tag_11"], name: "index_listings_on_tag_11"
    t.index ["tag_12"], name: "index_listings_on_tag_12"
    t.index ["tag_13"], name: "index_listings_on_tag_13"
    t.index ["tag_2"], name: "index_listings_on_tag_2"
    t.index ["tag_3"], name: "index_listings_on_tag_3"
    t.index ["tag_4"], name: "index_listings_on_tag_4"
    t.index ["tag_5"], name: "index_listings_on_tag_5"
    t.index ["tag_6"], name: "index_listings_on_tag_6"
    t.index ["tag_7"], name: "index_listings_on_tag_7"
    t.index ["tag_8"], name: "index_listings_on_tag_8"
    t.index ["tag_9"], name: "index_listings_on_tag_9"
  end

  create_table "sellers", force: :cascade do |t|
    t.string "name"
    t.integer "seller_id"
    t.bigint "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_sellers_on_user_id"
  end

  create_table "shops", force: :cascade do |t|
    t.integer "shop_id"
    t.string "shop_name"
    t.bigint "user_id"
    t.float "creation_tsz"
    t.string "title"
    t.string "announcement"
    t.boolean "is_vacation"
    t.float "last_updated_tsz"
    t.integer "listing_active_count"
    t.integer "digital_listing_count"
    t.string "login_name"
    t.string "url"
    t.string "image_url_760x100"
    t.integer "num_favorers"
    t.string "icon_url_fullxfull"
    t.boolean "is_using_structured_policies"
    t.bigint "listing_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "seller_id"
    t.index ["listing_id"], name: "index_shops_on_listing_id"
    t.index ["seller_id"], name: "index_shops_on_seller_id"
    t.index ["user_id"], name: "index_shops_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "password_digest"
    t.string "remember_digest"
    t.boolean "admin", default: false
    t.string "activation_digest"
    t.boolean "activated", default: false
    t.datetime "activated_at"
    t.string "reset_digest"
    t.datetime "reset_sent_at"
    t.string "request_token"
    t.string "request_secret"
    t.string "oauth_verifier"
    t.string "oauth_token"
    t.string "login_name"
    t.integer "user_id"
    t.index ["email"], name: "index_users_on_email", unique: true
  end

  add_foreign_key "sellers", "users"
  add_foreign_key "shops", "listings"
  add_foreign_key "shops", "sellers"
  add_foreign_key "shops", "users"
end
你能帮帮我吗? 我尝试了所有方法,包括在我创建Shop模型的前一天在应用程序中回滚,但显然它没有推出数据库。

我还尝试在本地克隆它以处理代码,但它克隆了最近的提交,而不是当前在应用程序中活动的提交。

提前致谢!

1 个答案:

答案 0 :(得分:2)

问题出现在这里:

-- add_foreign_key(:listings, :shop, {:column=>:shop_id})
ALTER TABLE "listings" 
  ADD CONSTRAINT "fk_rails_72ed4e1344"
  FOREIGN KEY ("shop_id")
  REFERENCES "shop" ("id") -- <<<< HERE, it should be `shops` instead of `shop`

add_foreign_key的第二个参数应该是表的名称,在你的情况下:

add_foreign_key :listings, :shops, column: :shop_id
#                               ^ plural