代码之家  ›  专栏  ›  技术社区  ›  user8359832

如何排除Heroku数据库错误?

  •  0
  • user8359832  · 技术社区  · 7 年前

    我的应用程序没有成功部署到Heroku,我收到一个数据库错误,上面说:

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

    似乎是说我没有topic_id的引用,但它在我的模式中。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 "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 "topics", force: :cascade do |t|
        t.string "title"
        t.datetime "created_at", null: false
        t.datetime "updated_at", null: false
      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
    

    如何排除与我在模式中看到的内容冲突的错误。rb文件?是错误告诉我进入postgres并添加约束项吗?如果是这样,我该怎么做?

    https://devcenter.heroku.com/articles/postgres-logs-errors

    PG::UndefinedTable: ERROR: relation "..." does not exist

    但我不确定这是否是答案。如果我理解上面的SO文档,我的主题表应该在blogs表之前创建,但这不是因为时间戳显示博客是在topics表之前创建的。当我研究它时,实际上blogs表是在迁移中的topics表之前创建的。

    那么,最干净、最简单的方法是什么来更正这些日期,以便首先创建表?假设这就是问题所在。

    我已经通过以下方式验证了主题表在本地确实存在:

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

    然而,当我这样做时:

    $ psql postgres
    psql (9.6.1)
    Type "help" for help.
    
    postgres=# SELECT * FROM pg_tables WHERE tablename = 'topics';
     schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
    ------------+-----------+------------+------------+------------+----------+-------------+-------------
    (0 rows)
    
    postgres=#
    

    我意识到,通过从一个社区成员那里获得的帮助,我需要切换到正确的数据库,一旦我切换到了,我就确认我有“主题”表:

    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)
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Community CDub    4 年前

    桌子 topics 在创建对它的FK引用之前,必须在那里。

    SELECT current_database();
    

    正在连接 psql postgres 数据库 名为“postgres”。(标准维护数据库,不要在那里创建对象,别管它。)

    SELECT *
    FROM   pg_tables
    WHERE  tablename = 'topics';
    

    在多个模式中可以有多个同名表。

    更多信息:

    模式( schemaname )出现在当前 search_path ?

    SHOW search_path;
    

    更多信息:

        2
  •  0
  •   Stacking For Heap    7 年前

    这个 REFERENCES "topics" ("id") 似乎引用了“topics”表和其中的字段“id”。我没有看到它是在create table上创建的?诚然,这已经有好几年了,但你不应该指的是现有的专栏吗?