[英]Rails 4 x postgresql: ActiveRecord::InvalidForeignKey
In my Rails 4 app, which uses postgresql
, I am running into an error that I cannot seem to understand: 在使用
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'
This is the code from comments_controller.rb
: 这是来自
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
And here is how the routes were setup: 这是设置路线的方式:
resources :posts, shallow: true do
resources :comments, shallow: true
end
resources :ads, shallow: true do
resources :comments, shallow: true
end
With a polymorphic association: 与多态关联:
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: 更新:
As per @RuNpiXelruN's request, here the DB schema for comments: 根据@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
————— -----
How can I fix the error I am getting in the logs? 如何解决日志中出现的错误?
The error is telling you that no post with id = 52 exists in the database when your trying to add a comment with a commentable_id = 52 . 该错误告诉您,当您尝试添加带有commentable_id = 52的注释时,数据库中没有id = 52的帖子。 I'm going to guess that column used to be post_id and was renamed in a migration when you swapped over to the polymorphic commentable relation?
我会猜测该列曾经是post_id,并且当您切换到多态可注释关系时在迁移中被重命名了吗? If so I think you still have a foreign key constraint in your database and will need to delete it.
如果是这样,我认为您的数据库中仍然存在外键约束,则需要删除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.