如何防止 Postgresql 中链表中的循环引用?

问题描述

表格和有效数据是这样的:

| id | after_id |
| -- | -------- |
| a  | null     |
| b  | a        |
| c  | b        |
| d  | c        |

我们的目标是防止类似的事情:

| id | after_id |
| -- | -------- |
| a  | d        | <- 'a' now follows 'd',creating a loop.
| b  | a        |
| c  | b        |
| d  | c        |

如果不存储一些有助于创建约束的附加信息,似乎不可能解决这个问题。但我无法弄清楚哪些信息会有所帮助。 Postgresql 有 EXCLUDE 约束,我想也许可以以某种方式使用重叠运算符。不知道如何解决这个问题,但我感觉它需要更新太多行,这将破坏将序列存储在链表中的全部意义。


更新: 这个想法是在数据库级别而不是应用程序级别确保列表的完整性。另一个无效状态的例子:

| id | after_id |
| -- | -------- |
| a  | null     |
| b  | d        | <- updated
| c  | b        |
| d  | c        |

或者其他:

| id | after_id |
| -- | -------- |
| a  | null     |
| b  | d        | <- updated: followed 'a',now follows 'd'
| c  | b        |
| d  | c        |
| e  | a        | <- updated: followed 'd',now follows 'a',unique constraint on after_id will not be violated this way

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)