简体   繁体   English

PG :: UndefinedColumn:错误:列comment.post_id不存在

[英]PG::UndefinedColumn: ERROR: column comments.post_id does not exist

I cant seem to figure out why I keep getting this error. 我似乎无法弄清楚为什么我继续收到此错误。 I am trying to add comment forms to a blog I am making and it wont work. 我试图在我正在创建的博客中添加评论表格,但它无法正常工作。 Here is what the full error says. 这是完整错误的说明。

ActiveRecord::StatementInvalid in Posts#show ActiveRecord :: StatementInvalid在Posts#show中

Showing /Users/ipbyrne/FirstRailsApp/blog/app/views/posts/show.html.erb where line #17 raised: 显示/Users/ipbyrne/FirstRailsApp/blog/app/views/posts/show.html.erb,其中第17行出现:

PG::UndefinedColumn: ERROR: column comments.post_id does not exist LINE 1: SELECT COUNT( ) FROM "comments" WHERE "comments"."post_id" ... ^ : SELECT COUNT( ) FROM "comments" WHERE "comments"."post_id" = $1 PG :: UndefinedColumn:错误:列注释.post_id不存在第1行:在“注释”中选择COUNT( )在“注释”中。“ post_id” ... ^:从“注释”中选择COUNT( )在“注释”中。“ post_id” = $ 1

</p>

   <div id="comments">
       <h2><%= @post.comments.count %> Comments</h2> <-- Says it breaks here
       <%= render @post.comments %>

       <h3>Add a comment:</h3>

From what i can understand there is a column missing in one of the tables in the schema.rb file? 据我了解,schema.rb文件中的表之一中缺少一列? Incase this the case here is what mine looks like 如果这种情况是我的样子

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140911230918) do

  create_table "comments", force: true do |t|
    t.string   "name"
    t.text     "body"
    t.integer  "post_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "comments", ["post_id"], name: "index_comments_on_post_id"

  create_table "posts", force: true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true 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"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

You probably just need to add the column into your comments table. 您可能只需要将该列添加到comments表中即可。

You can do that by creating a migration via the terminal to add the column in: 您可以通过在终端上创建迁移以在以下位置添加该列来做到这一点:

$ rails g migration add_post_id_column_to_comments post:belongs_to
$ rake db:migrate

The reason you want to use post:belongs_to is that Rails will automatically append the _id suffix and create a foreign key in the comments table to refer back to each other. 您要使用post:belongs_to的原因是,Rails会自动附加_id后缀并在注释表中创建一个外键以相互参考。

So essentially this part of the migration post:belongs_to will add the column post_id to your comments table. 因此,实质上,迁移post:belongs_to这一部分会将post_id列添加到您的注释表中。 (Same thing if you did for example cars:belongs_to , you would get cars_id , etc) (如果您执行例如cars:belongs_to ,也会得到cars_id等)

That way you can be able to refer to the post's comments like this: 这样,您就可以像这样引用帖子的评论:

@post.comments

The reason why your @post.comments is failing now is that it is looking for that post_id column that you have not made yet which is probably also because you may not have defined the relationship between your Post and Comment models. 您的@post.comments现在失败的原因是,它正在寻找您尚未创建的post_id列,这也可能是因为您可能尚未定义PostComment模型之间的关系。


If you haven't done so already you just need to quickly define the relationship in each model: 如果您尚未这样做,则只需在每个模型中快速定义关系即可:

  • A post has many comments. 帖子有很多评论。
  • A comment belongs to a post. 评论belongs to帖子。

Get the lingo? 懂行话吗?

In your Post model, just add 在您的Post模型中,只需添加

class Post < ActiveRecord::Base
    has_many :comments   # make sure it's pluralized
end

and in your Comment model, add 然后在您的Comment模型中添加

class Comment < ActiveRecord::Base
    belongs_to :post  # and this one is singularized
end

Then try running your app again. 然后尝试再次运行您的应用程序。 Let me know how it goes. 让我知道事情的后续。

Yes, you're missing the field post_id on the Comment model, which would tell Rails which comments belong to which posts. 是的,您在Comment模型上缺少字段post_id ,该字段会告诉Rails哪些注释属于哪些帖子。

You can add it by generating a migration from the command line like so: 您可以通过从命令行生成迁移来添加它,如下所示:

rails generate migration AddPostIdToComments post:references
rake db:migrate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PG::UndefinedColumn: 错误:courts.user_id 列不存在 - PG::UndefinedColumn: ERROR: column courts.user_id does not exist Rails PG::UndefinedColumn: 错误:列carts_parts.[:part_id, :cart_id] 不存在 - Rails PG::UndefinedColumn: ERROR: column carts_parts.[:part_id, :cart_id] does not exist ActionView :: Template :: Error(PG :: UndefinedColumn:错误:列“ user_id”不存在 - ActionView::Template::Error (PG::UndefinedColumn: ERROR: column “user_id” does not exist ActionView :: Template :: Error(PG :: UndefinedColumn:错误:列posts.user_id不存在 - ActionView::Template::Error (PG::UndefinedColumn: ERROR: column posts.user_id does not exist ActionView :: Template :: Error(PG :: UndefinedColumn:ERROR:列“weeknumber”不存在 - ActionView::Template::Error (PG::UndefinedColumn: ERROR: column “weeknumber” does not exist ActionView :: Template :: Error(PG :: UndefinedColumn:ERROR:列不存在) - ActionView::Template::Error (PG::UndefinedColumn: ERROR: column does not exist) PG :: UndefinedColumn:错误:关系“事件”的列“ city_id”不存在 - PG::UndefinedColumn: ERROR: column “city_id” of relation “events” does not exist PG :: UndefinedColumn:错误:ActiveAdmin上不存在列users.contact_id - PG::UndefinedColumn: ERROR: column users.contact_id does not exist on ActiveAdmin 活动管理员多对多显示索引 PG::UndefinedColumn: 错误: 列 case.product_id 不存在 - active admin many to many show index PG::UndefinedColumn: ERROR: column cases.product_id does not exist PG::UndefinedColumn: 错误:列 services.zip_code 不存在 - PG::UndefinedColumn: ERROR: column services.zip_code does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM