[英]PG::ForeignKeyViolation: ERROR: update or delete on table and I don't know why in my case
I am contacting you because I have a rather important problem.我联系你是因为我有一个相当重要的问题。 And that despite my research on stackoverflow or elsewhere, I can not correct after a lot of testing and a lot of tinkering.尽管我对 stackoverflow 或其他地方进行了研究,但经过大量测试和大量修补后,我无法纠正。
I practice by making a site where you can list secrets that I call "gossip".我通过创建一个可以列出我称之为“八卦”的秘密的网站来练习。
There is a button that allows you to delete a "gossip".有一个按钮可以让您删除“八卦”。
But when I want to remove the "gossip"但是当我想删除“八卦”时
I have an error message which is the following:我有一条错误消息,如下所示:
From what I understand, it is my relationship between the tables that is causing the problem.据我了解,是我的表之间的关系导致了问题。 I therefore attach you my code of these tables in questions:因此,我在问题中附上我对这些表格的代码:
class Tag < ApplicationRecord has_many:gossips, through: :tag_join_gossip, dependent: :destroy validates:title, presence: true end
class TagJoinGossip < ApplicationRecord belongs_to:gossip belongs_to:tag, optional: true end
class Gossip < ApplicationRecord belongs_to:user, optional: true has_many:likes has_many:tags, through: :tag_join_gossip has_many:comments validates:title, presence: true, length: {minimum: 3, maximum: 14} validates:content, presence: true, length: {minimum: 1} end
My controller:我的 controller:
class GossipController < ApplicationController before_action:authenticate_user def index @id = params[:id] @gossips = Gossip.all end def show @id= params[:id] @gossip = Gossip.find(params[:id]) @comments = Gossip.find(params[:id]).comments @comment = Comment.new(content: params['content'], gossip_id: 1, gossip_id: @gossip,user_id: 1) end def new @gossip = Gossip.new end def create @gossip = Gossip.new(title: params['title'], content: params['content']) @gossip.user = User.find_by(id: session[:user_id]) if @gossip.save flash[:success] = "Gossip bien créé:" redirect_to welcome_index_path else flash[.danger] = "Nous n'avons pas réussi à créer le potin pour la (ou les) raison(s) suivante(s) " render new_gossip_path end end def edit @gossip = Gossip:find(params[.id]) end def update @gossip = Gossip:find(params[.id]) gossip_params = params:require(.gossip):permit(,title: .content) if @gossip:update(gossip_params) flash[:success] = "Gossip bien modifié:" redirect_to welcome_index_path else render,edit flash[.danger] = "Nous n'avons pas réussi à modifier le potin. le titre doit faire 3 lettres minimum et vous devez remplir le contenu:" end end def destroy @gossip = Gossip.find(params[:id]) @gossip.destroy redirect_to welcome_index_path end end private def authenticate_user unless current_user flash[:danger] = "Veuillez vous connecter." redirect_to new_session_path end end
Maybe there are other files that can be of interest so I put my project's github here: https://github.com/MC4517/Projet-THP-GOSSIP-J1也许还有其他可能感兴趣的文件,所以我将项目的 github 放在这里: https://github.com/MC4517/Projet-THP-GOSSIP-J1
I've been on it all day, I'm starting to lose my mind lol.我整天都在忙,我开始失去理智了,哈哈。
Thank you very much for those who venture into my problem, although I think it must be easy for a large part of you!非常感谢那些冒险解决我的问题的人,尽管我认为这对你们中的大部分人来说一定很容易!
You have a record with a foreign key (TagJoinGossip) referencing the record you want to destroy (Gossip).您有一个带有外键 (TagJoinGossip) 的记录,它引用了您要销毁的记录 (Gossip)。 Adding dependent: :destroy to the has_many association should solve it.将dependent: :destroy 添加到 has_many 关联应该可以解决它。
Class Gossip < ApplicationRecord
has_many :tags, through: :tag_join_gossip, dependent: :destroy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.