Maayan Naveh
Maayan Naveh

Reputation: 370

Relation does not exist when deploying to heroku

I am trying to deploy a big change I made to my code into heroku.

When I run heroku run rails db:migrate I get the following error:

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")

Here is the offending migration file:

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

I cannot find what is causing this problem - the only thing I see is that in my heroku schema I have these lines:

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

When on my local schema I do not.

Here's the local schema:

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

Here is the heroku schema:

  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

Could you please help me? I tried everything, including rolling back in the app to the day before I created the Shop model, but apparently it doesn't roll the db.

I also tried cloning it locally to work on the code from there but it clones the most recent commit, not the one that is currently active in the app.

Thanks in advance!

Upvotes: 1

Views: 1428

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

The issue happens here:

-- 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`

The second argument of add_foreign_key is supposed to be the table's name, in your case:

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

Upvotes: 2

Related Questions