簡體   English   中英

PG :: UndefinedColumn:錯誤:列comment.post_id不存在

[英]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列,這也可能是因為您可能尚未定義PostComment模型之間的關系。


如果您尚未這樣做,則只需在每個模型中快速定義關系即可:

  • 帖子有很多評論。
  • 評論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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM