0

在我的Rails 4的應用程序,它使用postgresql,我遇到了一個錯誤,我似乎無法理解:Rails的4×PostgreSQL的:ActiveRecord的:: InvalidForeignKey

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 

帶AP olymorphic協會:

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 

-----

UPDATE:

按@ 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 

-----

我該如何解決我在日誌中遇到的錯誤?

+1

如果可以,請發佈您的數據庫架構 – RuNpiXelruN

+0

完成。這有幫助嗎? –

+0

你有沒有發表你的帖子?該錯誤表示您的帖子表中沒有commentable_id。 – RuNpiXelruN

回答

4

錯誤是告訴你,當您嘗試添加帶有commentable_id = 52的註釋時,數據庫中不存在ID爲52的帖子。我會猜測那個列曾經是post_id,並且在您轉換到多態可評論關係時在遷移中被重命名?如果是這樣,我認爲你的數據庫中仍然有一個外鍵約束,並且需要刪除它。

+0

你所說的一切都是正確的。我怎樣才能從數據庫中刪除外鍵約束? –

+2

沿線的東西。 ALTER TABLE評論DROP CONSTRAINT fk_rails_15236085 –

+0

好的,非常感謝。 –

相關問題