首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PG::ForeignKeyViolation:错误:对表进行更新或删除,但我不知道原因

PG::ForeignKeyViolation:错误:对表进行更新或删除,但我不知道原因
EN

Stack Overflow用户
提问于 2021-05-13 21:48:18
回答 1查看 31关注 0票数 0

我正在联系你,因为我有一个相当重要的问题。尽管我在stackoverflow或其他地方进行了研究,但在进行了大量测试和修补之后,我无法进行更正。

我通过制作一个网站来练习,在那里你可以列出我称之为“八卦”的秘密。

有一个按钮可以让你删除“八卦”。

但是当我想删除“八卦”的时候

我收到一条错误消息,如下所示:

据我所知,是我的表之间的关系导致了这个问题。因此,我将这些表的代码附加到您的问题中:

代码语言:javascript
复制
class Tag < ApplicationRecord
  has_many :gossips, through: :tag_join_gossip, dependent: :destroy

  validates :title, presence: true
end
代码语言:javascript
复制
class TagJoinGossip < ApplicationRecord
  belongs_to :gossip
  belongs_to :tag, optional: true
end
代码语言:javascript
复制
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

我的控制器:

代码语言:javascript
复制
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

也许还有其他可能感兴趣的文件,所以我将项目的github放在这里:https://github.com/MC4517/Projet-THP-GOSSIP-J1

我已经忙了一整天了,我开始发疯了,笑。

非常感谢那些探索我的问题的人,尽管我认为这对你们中的大部分人来说一定很容易!

EN

回答 1

Stack Overflow用户

发布于 2021-05-13 22:06:24

您有一个带有外键(TagJoinGossip)的记录,该记录引用了您想要销毁的记录(Gossip)。将dependent::destroy添加到has_many关联应该可以解决这个问题。

代码语言:javascript
复制
 Class Gossip < ApplicationRecord
 
     has_many :tags, through: :tag_join_gossip, dependent: :destroy
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67520380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档