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

GraphQL插入子节点关联父节点:如何为Notificacion关联Tienda

解决方案

要正确关联Notificacion到指定的Tienda,你需要在创建通知的input对象中传入自动生成的外键字段值,而非尝试单独传递store参数或嵌套对象。

正确代码写法

修改你的data对象,添加外键字段tiendaNewsId(从你的返回结果中可确认这是关联Tienda的外键):

const data = {
  content: "Nueva notificacion",
  tiendaNewsId: "55988776-11af-42b8-b93b-9de9af35f7dc" // 目标Tienda的ID
};

const response = await API.graphql(graphqlOperation(createNotificacion, { input: data }));

原因说明

在你使用的带Amplify指令的GraphQL Schema中,当定义Tienda的news: [Notificacion] @hasMany和Notificacion的store: Tienda @belongsTo时,Amplify会自动生成外键字段tiendaNewsId用于存储关联关系。

你之前的尝试存在以下问题:

  • 未将关联ID放入input对象内部,而是作为外部参数传递
  • 尝试直接传递store对象或ID,但CreateNotificacionInput结构不接受该字段,仅识别自动生成的外键字段

额外验证

若不确定外键字段名,可查看Amplify生成的API类型定义,CreateNotificacionInput中必然包含类似tiendaNewsId的字段(命名规则为父类型名+关系名+Id)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:25:36