gpt4 book ai didi

ruby-on-rails - Rails 4 x postgresql:ActiveRecord::InvalidForeignKey

转载 作者:行者123 更新时间:2023-11-29 14:06:40 26 4
gpt4 key购买 nike

在我使用 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 的要求,这里是评论的数据库模式:

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 并且在您切换到多态可注释关系时在迁移中被重命名?如果是这样,我认为您的数据库中仍有外键约束,需要将其删除。

关于ruby-on-rails - Rails 4 x postgresql:ActiveRecord::InvalidForeignKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com