EdgeDB自动反向链接异常:产品分类关联为空问题咨询
EdgeDB查询产品
categories链接为空的原因及解决方法 你的EdgeDB模式
module default { type Category { required title: str; multi subcategories: default::Category { constraint exclusive; }; multi products: default::Product { constraint exclusive; }; single parent := .<subcategories[is default::Category]; } type Product { required title: str; required offerId: str { constraint exclusive; }; required single category: default::Category; } }
问题现象
- 查询产品的
categories字段时,返回空链接 - 查询产品的
category字段时,链接正常返回
原因分析
你的Product类型中仅定义了名为category的必填单值链接,根本不存在categories这个字段。EdgeDB不会自动生成未定义的字段,所以查询不存在的categories自然返回空。
解决方法
情况1:查询时的笔误
直接将查询语句中的categories改为category即可,匹配你模式中定义的字段名。
情况2:需要获取多级分类(含父分类)
如果确实需要从产品出发获取其所属分类及所有父分类层级,可以在Product类型中添加一个计算属性来实现:
type Product { // 保留原有字段 required title: str; required offerId: str { constraint exclusive; }; required single category: default::Category; // 添加递归计算属性,获取当前分类及所有父分类 multi categories := ( recursive with get_parents(func: Category) := ( select func union get_parents(func.parent) ) select get_parents(.category) ); }
添加后,查询产品的categories就能得到该产品所属分类及其所有上级分类的集合。
内容的提问来源于stack exchange,提问作者Pavel Perevezencev
相关产品推荐
相关产品推荐

