【发布时间】:2021-07-09 03:27:59
【问题描述】:
由于将post.id 与post_reaction.post_id 关联的外键,我无法删除有反应(喜欢)的帖子。
我想在删除时使用 CASCADE 创建一个外键,但是因为一个帖子可以有很多 post_reactions,我收到一个错误:no unique constraint matching given keys for referenced table "post_reactions"
我不应该认为我必须在客户端单独删除所有内容,是吗?即先删除所有 post_reactions,然后删除帖子?
const handleDeletePost = () => {
if (postImageRelayId) {
ImageDeleteMutation(postImageRelayId); // if there is an image, delete it
}
if (postReactions.length) {
PostReactionDeleteMutation(postReactions); // if there are reactions, delete all of them
}
PostDeleteMutation(post.id, currentPerson); // delete post itself
};
图像表有一个 post_id 列,其 fkey 指向 post.id
post_reactions 表有一个 post_id 列,也有一个指向 post.id 的 fkey
我想简单地从帖子表中删除帖子,postgres CASCADE 删除任何具有该 post_id 的反应和/或图像,但是,我无法在引用 post_reactions 的帖子表上创建外键.post_id。
【问题讨论】:
-
请在您的问题中添加示例数据,以明确您在删除期间想要什么工作流程。
-
您需要将 post.id 标记为 PRIMARY KEY 才能正常工作。见:stackoverflow.com/questions/11966420/…
-
@TimBiegeleisen 非常感谢。我添加了我当前的删除函数,它调用了三个突变。我不确定哪些具体数据会有所帮助。
-
PostReactionDeleteMutation(postReactions)中的参数错误?在编码之前在操场上测试突变!
标签: sql postgresql graphql