简体   繁体   English

来自同一表的MySql外键(两列与一个键相关)

[英]MySql foreign key from the same table (2 columns are relevant to one key)

New question since I managed to find the answer. 自从我设法找到答案以来,出现了新问题。

The exercise wants us to create a comments table with reply levels. 练习希望我们创建一个带有回复级别的注释表。 there is a post ('id') that has comments ('comment_id') and there is a comment reply field ('reply_to') that needs to point to an existing comment ('comment_id') or NULL in case it is a comment to the post itself. 有一个带有评论('comment_id')的帖子('id'),并且有一个评论回复字段('reply_to')需要指向现有评论('comment_id')或NULL(如果是评论)到帖子本身。 'id' & 'comment_id' are the primary key 'user_id' is a foreign key for a different table i need a 2nd foreign key which is the reply field ('reply_to') and on delete of a comment the following comments should be deleted as well. 'id'和'comment_id'是主键'user_id'是不同表的外键,我需要第二个外键,这是回复字段('reply_to'),在删除评论时,以下评论应删除也一样

what I succeeded was setting the foreign key to 'comment_id' but then if there are many posts with the same comment number it causes chaos. 我成功的是将外键设置为'comment_id',但是如果有很多具有相同评论号的帖子,则会引起混乱。 deleting post #1 comment #1 will result in post #x comment #9 which is a reply to comment #5 (and not #1) to be deleted as well. 删除帖子#1评论#1将导致帖子#x评论#9,也就是对评论#5的回复(不是#1)。

How should I define the key so that only the right comment tree will be eliminated? 我应该如何定义键,以便只删除正确的注释树? (description of the db below) (以下数据库的说明)

Thanks 谢谢

Example: tbl 示例:tbl

post_id-----comment_id-----reply_to   
   0            0             NULL (to post)
   0            1              0
   1            0              NULL (to post)
   1            1              0

wanted action: 想要采取的行动:

Delete:       0            0             NULL (to post)

wanted result: 想要的结果:

post_id-----comment_id-----reply_to   
   1            0              NULL (to post)
   1            1              0

=========================================================================== ================================================== ========================

Fixed: needed to add indexing 固定:需要添加索引

The exercise wants us to create a comments table with reply levels. 练习希望我们创建一个带有回复级别的注释表。 there is a post ('id') that has comments ('comment_id') and there is a comment reply field ('reply_to') that needs to point to an existing comment ('comment_id') or NULL in case it is a comment to the post itself. 有一个带有评论('comment_id')的帖子('id'),并且有一个评论回复字段('reply_to')需要指向现有评论('comment_id')或NULL(如果是评论)到帖子本身。 'id' & 'comment_id' are the primary key 'user_id' is a foreign key for a different table i need a 2nd foreign key which is the reply field ('reply_to') and on delete of a comment the following comments should be deleted as well. 'id'和'comment_id'是主键'user_id'是不同表的外键,我需要第二个外键,这是回复字段('reply_to'),在删除评论时,以下评论应删除也一样

I am trying to add a foreign key from the same table but I keep getting this error: 我正在尝试从同一张表中添加外键,但我一直收到此错误:

#1005 - Can't create table db.#sql-148f_1027d34' (errno: 150)

Here is the definition of the table: 这是表的定义:

CREATE TABLE IF NOT EXISTS `PostComments` (
  `id` int(11) NOT NULL,
  `comment_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `content` varchar(512) COLLATE utf8_unicode_ci NOT NULL,
  `reply_to` int(11) DEFAULT NULL,
  `rating` int(11) NOT NULL DEFAULT '0',
  `report_counter` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`,`comment_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


ALTER TABLE `PostComments`
  ADD CONSTRAINT `PostComments_ibfk_1` FOREIGN KEY (`id`) REFERENCES `Posts` (`id`),
  ADD CONSTRAINT `PostComments_ibfk_2` FOREIGN KEY (`id`) REFERENCES `Posts` (`id`),
  ADD CONSTRAINT `PostComments_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `Users` (`id`);

this is what i am trying to do: 这就是我想要做的:

ALTER TABLE `PostComments`
ADD CONSTRAINT `fk_ReplyToT`
FOREIGN KEY (`reply_to`)
REFERENCES `PostComments` (`comment_id`)
ON DELETE CASCADE

After strolling in a few different threads I tried deleting all of the entries in this table but still no success. 在几个不同的线程中漫步后,我尝试删除该表中的所有条目,但仍然没有成功。 Also the fields are with the same type (INT) 而且这些字段具有相同的类型(INT)

what else am I possibly missing? 我还可能想念什么? Thanks in advance! 提前致谢!

You are trying to reference two times the same column from the table postcomments with the same id in the table posts: 您正在尝试从表后注中两次引用表后记中具有相同ID的同一列:

ALTER TABLE `PostComments`
  ADD CONSTRAINT `PostComments_ibfk_1` FOREIGN KEY (`id`) REFERENCES `Posts` (`id`),
  ADD CONSTRAINT `PostComments_ibfk_2` FOREIGN KEY (`id`) REFERENCES  `Posts` (`id`),
  ADD CONSTRAINT `PostComments_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `Users` (`id`);

That has no use. 那没用。

The first ( id ) in the FOREIGN KEY statement (so FOREIGN KEY ( id ) ) is the column in the table you are applying the constraint on ( the postcomments table in this case). FOREIGN KEY语句中的第一个( id )(因此FOREIGN KEY id )是要对其施加约束的表中的列(在这种情况下为postcomments表)。

So replace the id of your second constraint (of first, does not matter) with the column you actually want to place the constraint on. 因此,将第二个约束的ID(第一个约束无所谓)替换为您实际要在其上放置约束的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM