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

时间:2019-05-02 16:18:16

标签: ruby-on-rails ruby heroku pg

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

我在Rails 5.2.3和ruby 2.3.3上

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

第1行:从“帖子”中的“帖子”中选择COUNT(*)。“ 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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

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

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

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

如果您使用seed.rb文件构建种子,那么您应该可以运行

heroku run rake db:seed

答案 2 :(得分:0)

不确定在部署到heroku时是否可以设置自动迁移, 但是将代码部署到heroku之后,您需要运行

heroku run rake db:migrate

您的问题将得到解决。