You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查询TypeDB关系中定义的角色类型及类型约束?

查询TypeDB中关系类型的角色扮演者约束

假设在schema中定义了如下内容:

define
person sub entity, plays employment:employee;
company sub entity, plays employment:employer;
employment sub relation, relates employee, relates employer;

上述Schema定义的约束为:employer角色必须由company类型实体扮演,employee角色必须由person类型实体扮演。
使用基础查询match $x sub relation; get $x;仅能返回所有关系类型(比如上述例子里的employment),无法获取关系关联的角色、以及角色对应的合法扮演者类型,需要使用带relates和plays条件的查询语句获取完整约束。


可用查询语句

查询所有关系及其关联角色

如果只需要拿到每个关系下定义的角色,使用如下查询:

match
$rel sub relation;
$rel relates $role;
get $rel, $role;

针对示例Schema,该查询返回结果为:

  • 关系employment,关联角色employee
  • 关系employment,关联角色employer

查询角色对应的合法扮演者类型

如果需要拿到每个角色允许的实体扮演者类型,在查询中补充plays条件关联实体类型即可:

match
$rel sub relation;
$rel relates $role;
$playerType sub entity, plays $role;
get $rel, $role, $playerType;

针对示例Schema,该查询会返回完整的约束结果:

  • 关系employment的employer角色,合法扮演者类型为company
  • 关系employment的employee角色,合法扮演者类型为person

查询指定关系的约束

如果只需要查询某一个特定关系的角色约束,可以直接指定关系类型过滤,例如查询employment的约束:

match
$rel type employment;
$rel relates $role;
$playerType sub entity, plays $role;
get $role, $playerType;

内容的提问来源于stack exchange,提问作者Inspired_Blue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 11:06:20