ruby-on-rails - ActionView::Template::Error (PG::UndefinedColumn: ERROR: column posts.user_id does not exist

标签 ruby-on-rails ruby heroku pg

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

我在 Rails 5.2.3 和 ruby​​ 2.3.3 上

ActionView::Template::Error (PG::UndefinedColumn: ERROR: column posts.user_id does not exist

第 1 行:SELECT COUNT(*) FROM "posts"WHERE "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

最佳答案

heroku pg:reset
heroku run rake db:migrate
heroku restart

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

关于ruby-on-rails - ActionView::Template::Error (PG::UndefinedColumn: ERROR: column posts.user_id does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55956517/

相关文章:

Python Flask heroku 应用程序错误

ruby-on-rails - 如何在 ruby​​ on rails 中创建用户定义的重定向?

ruby-on-rails - 在网络应用程序中使用开源代码/软件

ssl - Heroku ssl 证书 : Key could not be read since it's protected by a passphrase

ruby-on-rails - 使用 CSS 和 javascript 的 Rails gem

ruby - 在 rspec 中指定内容类型

ruby-on-rails - 需要在特定时间运行作业

ruby-on-rails - 添加字段以使用 Rails 4 设计注册

用于 Ruby On Rails 的 MySQL Gem

javascript - 使用 x 可编辑内联模式自动保存在数据库中,无需在表上添加提交按钮