Flutter 中的 ModelMutations.update() 方法对 DynamoDB 是否需要主键?

0

【以下的问题经过翻译处理】 参考官方文档,我试图通过.create()成功添加的项,使用.update()进行更新。收到的变异响应显示了我成功的尝试,但实际上并没有进行任何更新。

试图进行简单的更改,如改变布尔值,但结果相同。

然后我想起了从lambda进行更改需要使用.key()方法来检查要更新的项的主键。在flutter / Dart中是否有相同的检查机制?如果有,那么实现的样子会是什么呢?

profile picture
EXPERTE
gefragt vor 8 Monaten88 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 感谢您就以上查询与我们联系。我想分享一下,为了执行更新/删除操作,必须需要现有记录的主键才能使变更成功。

例如,考虑以下示例模式,其中"id"将被用作主键:

type Todo @model {
  id: ID!
  name: String!
  description: String
}

现在,在我的测试设置中,我以两种方式执行并测试了更新变更:

1.在同一函数中创建和更新变更:

//Creating a record
Todo todo = Todo(name: 'my first todo', description: 'todo description');
final request = ModelMutations.create(todo);
final response = await Amplify.API.mutate(request: request).response;
Todo? createdTodo = response.data;

//Updating a record using the returned response for .create()
final todoWithNewName = createdTodo.copyWith(name: 'new todo');
final request1 = ModelMutations.update(todoWithNewName);
final response1 = await Amplify.API.mutate(request: request1).response;
Todo? updatedTodo = response1.data;

2.在单独的函数中进行更新变更

//At first, fetching the existing record with id
final request = ModelQueries.get(Todo.classType,"<passing_record_id>");
final response = await Amplify.API.query(request: request).response;
Todo? createdTodo = response.data;

//then, updating the fetched record 
final todoWithNewName = createdTodo.copyWith(name: 'updated todo');
final request1 = ModelMutations.update(todoWithNewName);
final response1 = await Amplify.API.mutate(request: request1).response;
Todo? updatedTodo = response1.data;

在两个情况下发出请求时,我能够观察到更新变更是按预期执行的。但是,当使用不正确的记录发出请求(即id值错误或没有id)时,我发现响应返回null。请注意,上述代码仅供参考。

另外,请注意 AWS API 插件不支持冲突检测。如果 AppSync 返回有关缺少 _version 和 _lastChangedAt 字段的错误,或者有关未处理冲突的错误,那么我们将不得不在我们的 API 中禁用冲突检测。

请参考以下链接,这些链接可能对解决此问题有帮助:

https://docs.amplify.aws/lib/graphqlapi/getting-started/q/platform/flutter/#configure-api https://docs.amplify.aws/lib/graphqlapi/mutate-data/q/platform/flutter/ https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/flutter/ GitHub 问题讨论:https://github.com/aws-amplify/amplify-flutter/issues/1434 如果您面临进一步的挑战,请随时使用以下链接向 AWS 开启支持案例。

profile picture
EXPERTE
beantwortet vor 8 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen