Search code examples
ruby-on-railsrubyherokupg

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


my code is working fine on my system in development mode but when i pushed to heroku, i am getting this error on the logs.

i'm on Rails 5.2.3 & ruby 2.3.3

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

LINE 1: SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = $1

On heroku console, when i try to retrieve user_id i get

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

but on development i get

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>

this is my 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

Post Model

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

user model

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

Solution

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

    There is no instances of Post which gives you the error of NilClass. You can seed the database as suggested in the other post. For more information about seeding a database check the rails docs on seeding.