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

如何解决AppSync中‘表不存在指定索引phoResultsByUserIdAndCreatedAt’的报错?

问题:执行phoResultsByUserIdAndCreatedAt查询时提示索引不存在

错误信息

The table does not have the specified index: phoResultsByUserIdAndCreatedAt (Service: DynamoDb, Status Code: 400, Request ID: FI38DQLTQEEV0KIPBPRFSU9JJVVV4KQNSO5AEMVJF66Q9ASUAAJG)

Schema定义

type PhoResult @model @auth(rules: [{ allow: private, operations: [read, create, update, delete] }]) {
  id: ID! @primaryKey
  userId: ID! @index(sortKeyFields: ["createdAt"])
  materialId: ID!
  material: PhoMaterial @hasOne(fields: ["materialId"])
  materialGroupId: ID @index(sortKeyFields: ["createdAt"])
  materialGroup: PhoMaterialGroup @hasOne(fields: ["materialGroupId"])
  category: String
  kind: String! # normal / own
  topic: String
  level: Int
  lesson: Int
  group: String
  sentence: String!
  ipaSentence: String!
  ipaSentenceResult: String!
  qualityScore: Float
  soundFile: String
  wordList: [WordScoreList]
  lgmScoreTotal: Float
  lgmScorePhone: Float
  lgmScoreFrequency: Float
  lgmScoreStress: Float
  favoriteFlg: Int
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime
}

查询语句

query MyQuery {
  phoResultsByUserIdAndCreatedAt(userId: "7328af1f-9aa3-4782-9326-a19f19c7b86d", createdAt: {ge: "2023-07-01"}) {
    items {
      id
    }
  }
}

解决方法

1. 重新部署Amplify更新数据库结构

修改Schema后,必须执行部署命令让Amplify同步DynamoDB表的索引:

amplify push

部署时确认修改数据库的提示,选择Yes完成更新。

2. 显式指定索引名称(确保匹配查询需求)

如果默认生成的索引名称与查询期望的不一致,在userId字段的@index注解中显式指定索引名:

userId: ID! @index(name: "phoResultsByUserIdAndCreatedAt", sortKeyFields: ["createdAt"])

修改后再次执行amplify push部署,DynamoDB会创建指定名称的全局二级索引(GSI)。

3. 验证索引状态

登录AWS控制台,进入DynamoDB服务,找到PhoResult表,在“索引”标签页确认phoResultsByUserIdAndCreatedAt索引存在且状态为“活动”。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 19:20:56