表格关系如何存在但PG不同时存在?

时间:2017-10-03 01:45:01

标签: ruby-on-rails postgresql heroku

我的应用没有成功部署到Heroku,我收到的数据库错误说明了这一点:

    D, [2017-10-03T01:27:39.481363 #4] DEBUG -- :    (0.8ms)  SELECT pg_try_advisory_lock(6469487183789743890)
D, [2017-10-03T01:27:39.498209 #4] DEBUG -- :    (1.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
I, [2017-10-03T01:27:39.500391 #4]  INFO -- : Migrating to AddTopicReferenceToBlogs (20170725215733)
D, [2017-10-03T01:27:39.503471 #4] DEBUG -- :    (0.7ms)  BEGIN
== 20170725215733 AddTopicReferenceToBlogs: migrating =========================
-- add_reference(:blogs, :topic, {:foreign_key=>true})
D, [2017-10-03T01:27:39.506450 #4] DEBUG -- :    (1.3ms)  ALTER TABLE "blogs" ADD "topic_id" bigint
D, [2017-10-03T01:27:39.516648 #4] DEBUG -- :    (4.5ms)  CREATE  INDEX  "index_blogs_on_topic_id" ON "blogs"  ("topic_id")
D, [2017-10-03T01:27:39.523496 #4] DEBUG -- :    (4.8ms)  ALTER TABLE "blogs" ADD CONSTRAINT "fk_rails_7f5637ea0d"
FOREIGN KEY ("topic_id")
  REFERENCES "topics" ("id")

D, [2017-10-03T01:27:39.524434 #4] DEBUG -- :    (0.7ms)  ROLLBACK
D, [2017-10-03T01:27:39.525504 #4] DEBUG -- :    (0.8ms)  SELECT pg_advisory_unlock(6469487183789743890)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "topics" does not exist
: ALTER TABLE "blogs" ADD CONSTRAINT "fk_rails_7f5637ea0d"
FOREIGN KEY ("topic_id")
  REFERENCES "topics" ("id")

它位于我的schema.rb文件中:

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

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

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

  create_table "blogs", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "slug"
    t.integer "status", default: 0
    t.bigint "topic_id"
    t.index ["slug"], name: "index_blogs_on_slug", unique: true
    t.index ["topic_id"], name: "index_blogs_on_topic_id"
  end

  create_table "comments", force: :cascade do |t|
    t.text "content"
    t.bigint "user_id"
    t.bigint "blog_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["blog_id"], name: "index_comments_on_blog_id"
    t.index ["user_id"], name: "index_comments_on_user_id"
  end

  create_table "friendly_id_slugs", force: :cascade do |t|
    t.string "slug", null: false
    t.integer "sluggable_id", null: false
    t.string "sluggable_type", limit: 50
    t.string "scope"
    t.datetime "created_at"
    t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
    t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
    t.index ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id"
    t.index ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type"
  end

  create_table "portfolios", force: :cascade do |t|
    t.string "title"
    t.string "subtitle"
    t.text "body"
    t.text "main_image"
    t.text "thumb_image"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "position"
  end

  create_table "skills", force: :cascade do |t|
    t.string "title"
    t.integer "percent_utilized"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "badge"
  end

  create_table "technologies", force: :cascade do |t|
    t.string "name"
    t.bigint "portfolio_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["portfolio_id"], name: "index_technologies_on_portfolio_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    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.inet "current_sign_in_ip"
    t.inet "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "roles"
    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

  add_foreign_key "blogs", "topics"
  add_foreign_key "comments", "blogs"
  add_foreign_key "comments", "users"
  add_foreign_key "technologies", "portfolios"
end

当我最初收到错误时,我将“主题”表移到“博客”表之前,相信订单会有所作为。它没有。

我已经确认主题表确实存在于此处:

ActiveRecord::Base.connection.tables
 => ["schema_migrations", "ar_internal_metadata", "friendly_id_slugs", "skills", "portfolios", "users", "topics", "blogs", "comments", "technologies"]
2.3.3 :002 >

在这里:

postgres=# \connect <db_name>_development
You are now connected to database "<db_name>_development" as user "danale".
<db_name>_development=# SELECT * FROM pg_tables WHERE tablename = 'topics';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | topics    | danale     |            | t          | f        | t           | f
(1 row)

那么为什么我在运行时会继续出现此错误?

heroku run rake db:migrate

如果我在Postgres中运行它:

# ALTER TABLE "blogs" ADD CONSTRAINT "fk_rails_7f5637ea0d" FOREIGN KEY ("topic_id") REFERENCES "topics" ("id");

它告诉我这种关系确实存在:

ERROR:  constraint "fk_rails_7f5637ea0d" for relation "blogs" already exists

这是我的迁移文件: enter image description here

2 个答案:

答案 0 :(得分:2)

我想您现在已经找到答案了……但是似乎我们正在同一道课上,克隆了Jordan H.投资组合后,我终于设法使它起作用。

无论如何,我在开发模式中遇到了这个问题,终于找到了!

首先,放下桌子

rails db:drop

创建一个新的

rails db:create

结束然后运行迁移

rails db:migrate

然后它应该像对我一样起作用!

答案 1 :(得分:1)

过去搞砸了迁移文件,现在您回答了共享迁移文件的答案。你有大约三个具有相似名称的迁移文件,我知道在Ruby on Rails中这不一定是个问题。你可能不得不这样做,因为你弄乱了某些东西,去过那里,做了那件事,但是在部署到heroku时它的不同之处。

删除部分重复的迁移文件,以清理迁移文件。我很惊讶错误不是重复错误。我认为这对你来说会更清楚。

无论如何,清理它,确保保存更改,然后执行:

git push heroku master

再次上传文件,然后:

heroku run rake db:migrate

再次将数据库迁移到heroku。最后,为了更好的衡量,我也会做:

heroku restart

然后交叉你的手指。