![](/img/trans.png)
[英]Rails ActiveRecord::InvalidForeignKey in ArticlesController#destroy
[英]Rails 4 x postgresql: ActiveRecord::InvalidForeignKey
在使用postgresql
Rails 4應用程序中,我遇到了我似乎無法理解的錯誤:
Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.2ms)
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: insert or update on table "comments" violates foreign key constraint "fk_rails_2fd19c0db7"
DETAIL: Key (commentable_id)=(52) is not present in table "posts".
: INSERT INTO "comments" ("body", "commentable_id", "commentable_type", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"):
app/controllers/comments_controller.rb:52:in `block in create'
app/controllers/comments_controller.rb:51:in `create'
這是來自comments_controller.rb
的代碼:
def create
@commentable = load_commentable
@comment = @commentable.comments.build(comment_params)
@comment.user_id = current_user.id
respond_to do |format|
if @comment.save
format.html { redirect_to :back }
format.json { render :show, status: :created, location: @comment }
format.js
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
這是設置路線的方式:
resources :posts, shallow: true do
resources :comments, shallow: true
end
resources :ads, shallow: true do
resources :comments, shallow: true
end
與多態關聯:
class Post < ActiveRecord::Base
has_many :comments, as: :commentable, dependent: :destroy
end
class Ad < ActiveRecord::Base
has_many :comments, as: :commentable, dependent: :destroy
end
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
-----
更新:
根據@RuNpiXelruN的請求,請在以下DB模式中進行注釋:
create_table "comments", force: :cascade do |t|
t.integer "user_id"
t.string "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "commentable_id"
t.string "commentable_type"
end
create_table "ads", force: :cascade do |t|
t.string "campaign"
t.string "text"
t.string "headline"
t.text "description"
t.string "url"
t.string "cta"
t.text "context"
t.string "status"
t.string "approval"
t.integer "calendar_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
create_table "posts", force: :cascade do |t|
t.integer "calendar_id"
t.datetime "date"
t.string "subject"
t.string "format"
t.text "copy"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "short_copy"
t.integer "score"
t.boolean "facebook"
t.boolean "twitter"
t.boolean "instagram"
t.boolean "pinterest"
t.boolean "google"
t.boolean "linkedin"
t.boolean "tumblr"
t.boolean "snapchat"
t.string "approval"
end
-----
如何解決日志中出現的錯誤?
該錯誤告訴您,當您嘗試添加帶有commentable_id = 52的注釋時,數據庫中沒有id = 52的帖子。 我會猜測該列曾經是post_id,並且當您切換到多態可注釋關系時在遷移中被重命名了嗎? 如果是這樣,我認為您的數據庫中仍然存在外鍵約束,則需要刪除它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.