![](/img/trans.png)
[英]PG::UndefinedColumn: ERROR: column courts.user_id does not exist
[英]PG::UndefinedColumn: ERROR: column comments.post_id does not exist
我似乎无法弄清楚为什么我继续收到此错误。 我试图在我正在创建的博客中添加评论表格,但它无法正常工作。 这是完整错误的说明。
ActiveRecord :: StatementInvalid在Posts#show中
显示/Users/ipbyrne/FirstRailsApp/blog/app/views/posts/show.html.erb,其中第17行出现:
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>
据我了解,schema.rb文件中的表之一中缺少一列? 如果这种情况是我的样子
# 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
您可能只需要将该列添加到comments
表中即可。
您可以通过在终端上创建迁移以在以下位置添加该列来做到这一点:
$ rails g migration add_post_id_column_to_comments post:belongs_to
$ rake db:migrate
您要使用post:belongs_to
的原因是,Rails会自动附加_id
后缀并在注释表中创建一个外键以相互参考。
因此,实质上,迁移post:belongs_to
这一部分会将post_id
列添加到您的注释表中。 (如果您执行例如cars:belongs_to
,也会得到cars_id
等)
这样,您就可以像这样引用帖子的评论:
@post.comments
您的@post.comments
现在失败的原因是,它正在寻找您尚未创建的post_id
列,这也可能是因为您可能尚未定义Post
和Comment
模型之间的关系。
如果您尚未这样做,则只需在每个模型中快速定义关系即可:
belongs to
帖子。 懂行话吗?
在您的Post
模型中,只需添加
class Post < ActiveRecord::Base
has_many :comments # make sure it's pluralized
end
然后在您的Comment
模型中添加
class Comment < ActiveRecord::Base
belongs_to :post # and this one is singularized
end
然后尝试再次运行您的应用程序。 让我知道事情的后续。
是的,您在Comment
模型上缺少字段post_id
,该字段会告诉Rails哪些注释属于哪些帖子。
您可以通过从命令行生成迁移来添加它,如下所示:
rails generate migration AddPostIdToComments post:references
rake db:migrate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.