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

Amplify GraphQL中@belongsTo指令报错:不接受部分排序键

解决Amplify GraphQL关联报错问题

你的问题出在单个@belongsTo字段不能同时关联两个外键ID,Amplify的@belongsTo指令要求每个关联只能对应一个父模型的外键,你试图用一个currency字段绑定fromCurrencyId和toCurrencyId两个外键,这违反了关联规则,所以触发了Invalid @belongsTo directive on currency. Partial sort keys are not accepted.错误。

修正后的模型定义

把单个关联拆成两个独立的@belongsTo关联,分别对应fromCurrency和toCurrency:

type Advert @model @auth(rules: [{allow: public}]) {
  id: ID!
  fromCurrency: Currency @belongsTo(fields: ["fromCurrencyId"])
  fromCurrencyId: ID!
  toCurrency: Currency @belongsTo(fields: ["toCurrencyId"])
  toCurrencyId: ID!
}

type Currency @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  code: String!
  symbol: String!
  # 可选:双向关联,查询该货币作为fromCurrency的所有广告
  fromAdverts: [Advert] @hasMany(fields: ["fromCurrencyId"])
  # 可选:双向关联,查询该货币作为toCurrency的所有广告
  toAdverts: [Advert] @hasMany(fields: ["toCurrencyId"])
}

关键说明

  • 每个@belongsTo字段单独绑定一个外键ID,这样每个关联都是合法的一对一父模型关联。
  • Currency模型中的fromAdverts和toAdverts是双向关联字段,如果你不需要从Currency反向查询广告,可以直接删除这两个字段,不影响Advert模型的关联查询功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 15:42:41