ActionView::Template::Error (PG::UndefinedColumn: ERROR: 列posts.user_id 不存在

Posted

技术标签:

【中文标题】ActionView::Template::Error (PG::UndefinedColumn: ERROR: 列posts.user_id 不存在【英文标题】:ActionView::Template::Error (PG::UndefinedColumn: ERROR: column posts.user_id does not exist 【发布时间】:2019-09-21 05:10:45 【问题描述】:

我的代码在开发模式下在我的系统上运行良好,但是当我推送到 heroku 时,我在日志中收到此错误。

我在 Rails 5.2.3 和 ruby​​ 2.3.3 上

第 1 行:从 "posts" 中选择 COUNT(*),其中 "posts"."user_id" = $1

在 heroku 控制台上,当我尝试检索 user_id 时,我得到了

irb(main):001:0> p = Post.last
  Post Load (11.0ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> nil
irb(main):002:0> p.user_id
Traceback (most recent call last):
        1: from (irb):2
NoMethodError (undefined method `user_id' for nil:NilClass)
irb(main):003:0>  !    ECONNRESET: read ECONNRESET

但在开发中我得到了

irb(main):001:0> p =Post.last
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT ?  [["LIMIT", 1]]
=> #<Post id: 13, description: "#snopp", user_id: 1, created_at: "2019-05-02 15:38:09", updated_at: "2019-05-02 15:38:09", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>
irb(main):002:0> p.user_id
=> 1
irb(main):003:0>

这是我的 schema.rb

ActiveRecord::Schema.define(version: 2019_05_02_123348) do

  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.integer "record_id", null: false
    t.integer "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

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

  create_table "post_hash_tags", force: :cascade do |t|
    t.integer "post_id"
    t.integer "hash_tag_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["hash_tag_id"], name: "index_post_hash_tags_on_hash_tag_id"
    t.index ["post_id"], name: "index_post_hash_tags_on_post_id"
  end

  create_table "posts", force: :cascade do |t|
    t.string "description"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "image_file_name"
    t.string "image_content_type"
    t.bigint "image_file_size"
    t.datetime "image_updated_at"
  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.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "username"
    t.string "name"
    t.string "website"
    t.text "bio"
    t.integer "phone"
    t.string "gender"
    t.string "avatar"
    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

end

后模型

class Post < ApplicationRecord
    after_commit :create_hash_tags, on: :create

    has_many :post_hash_tags
    has_many :hash_tags, through: :post_hash_tags



    belongs_to :user
end

用户模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :trackable

         has_many :posts, dependent: :destroy
end

【问题讨论】:

在生产中,您尚未在 posts 表中创建任何内容。 Post.last == nil. 错误不是说posts.user_id does not exist。错误显示undefined method 'user_id' for nil:NilClass 当我尝试posts/new时,我得到ActiveModel::UnknownAttributeError(Post的未知属性'user_id'。) 您的数据库中似乎没有帖子。尝试运行Post.count,我猜它会返回0 【参考方案1】:
heroku pg:reset
heroku run rake db:migrate
heroku restart

没有Post 的实例,这会给您NilClass 的错误。您可以按照另一篇文章中的建议为数据库播种。有关播种数据库的更多信息,请查看rails docs on seeding.

【讨论】:

该错误并不表示这是解决方案。如果没有帖子表,错误将是PG::UndefinedTable: ERROR 给出 undefined method 'user_id' for nil:NilClass,这意味着 Heroku 会记录该错误,因为没有 Post 实例。甚至认为班级在那里 @lacostenycoder 你看!,你投了一个正确的答案。 @bottles 如果是正确的,你介意支持解决方案。 ????????【参考方案2】:

要在我们假设在生产模式下运行的 Heroku 上测试您的应用程序,您可能需要为生产数据库播种。

您应该有一个db/seeds.rb 文件,您可以在其中执行此操作。

请参阅this answer 了解使用方法。 另请参阅 Rails Docs 了解想法。

如果您使用种子.rb 文件构建种子,您应该能够运行

heroku run rake db:seed

【讨论】:

谢谢,我很感激。我会调查的【参考方案3】:

不确定我们是否可以在部署到 heroku 时设置自动迁移, 但是一旦代码部署到heroku,你需要运行

heroku run rake db:migrate

你的问题就解决了。

【讨论】:

以上是关于ActionView::Template::Error (PG::UndefinedColumn: ERROR: 列posts.user_id 不存在的主要内容,如果未能解决你的问题,请参考以下文章