如何解决EdgeDB的‘constraints cannot contain paths with more than one hop’错误
问题
我在.esdl文件中编写了极简示例代码,试图通过约束限制Combo类型中obj1与obj2的类型组合(例如不支持A与A组合,支持A与B组合),但触发错误:constraints cannot contain paths with more than one hop。我参考了官方文档,且未使用multi属性,仍无法解决问题。使用的是EdgeDB v5.6。
ESDL代码示例
abstract type Base; type A extending Base; type B extending Base; type C extending Base; type D extending Base; type Combo { obj1: Base; obj2: Base; # 我的目标是允许某些类型组合,禁止其他组合。 # 例如:不支持A+A,但支持A+B。 constraint expression on ( not (.obj1 is A and .obj2 is A) or not (.obj1 is A and .obj2 is D) or not (.obj1 is B and .obj2 is C) ); # 我尝试过的另一种写法,同样报错 # constraint expression on ( # not (__subject__.obj1 is typeof A and __subject__.obj2 is typeof A) or # not (__subject__.obj1 is typeof A and __subject__.obj2 is typeof D) or # not (__subject__.obj1 is typeof B and __subject__.obj2 is typeof C) # ); }
错误信息
error: constraints cannot contain paths with more than one hop ┌─ /<path>/default.esdl:11:9 │ 11 │ ╭ constraint expression on ( 12 │ │ not (.obj1 is A and .obj2 is A) or 13 │ │ not (.obj1 is A and .obj2 is D) or 14 │ │ not (.obj1 is B and .obj2 is C) 15 │ │ ); │ ╰──────────^ error
解决方案
1. 调整约束为全局约束
EdgeDB v5.x中,类型内部的constraint expression仅允许引用单个属性路径,无法同时关联多个属性(如obj1和obj2)。要实现跨属性的组合约束,需要将约束定义为全局约束,放在类型定义外部:
abstract type Base; type A extending Base; type B extending Base; type C extending Base; type D extending Base; type Combo { obj1: Base; obj2: Base; } # 全局约束:禁止指定的类型组合 constraint expression on Combo ( not ( (.obj1 is A and .obj2 is A) or (.obj1 is A and .obj2 is D) or (.obj1 is B and .obj2 is C) ) );
2. 修正逻辑表达式
原约束逻辑存在错误:not X or not Y or not Z等价于not (X and Y and Z),这意味着只有当三个禁止组合同时满足时才会触发约束(实际不可能)。正确逻辑应为禁止任意一个指定组合,即使用not (X or Y or Z)(等价于not X and not Y and not Z),只要匹配任意一个禁止组合就会被拦截。
额外优化
如果需要更严谨的类型校验,可使用typeof操作符明确检查类型:
constraint expression on Combo ( not ( (typeof .obj1 = typeof A and typeof .obj2 = typeof A) or (typeof .obj1 = typeof A and typeof .obj2 = typeof D) or (typeof .obj1 = typeof B and typeof .obj2 = typeof C) ) );
内容的提问来源于stack exchange,提问作者Friendly King
相关产品推荐
相关产品推荐

