Open caridavidson opened 1 year ago
I have this issue as well using Datastore with Expo React Native.
WARN [WARN] 52:05.128 DataStore - conflict trycatch [Error: Field userID is required]
It happens to my code when using Optimistic Concurrency and sending updates. I am correctly importing the models generated in ./src/models. It might be linked to sending updates in rapid succession while using Optimistic Concurrency?
Use Case:
const original = await DataStore.query(Exercise, updatedExercise.id);
await DataStore.save(
Exercise.copyOf(original, (updated) => {
updated.variationID = updatedExercise.variationID;
updated.sets = JSON.stringify(updatedExercise.sets);
updated.userID = user.username; //The error occurs when this line is here and when it is not here
})
);
Respective Schema:
type Exercise @model @auth(rules: [{ allow: owner }]) {
id: ID! @primaryKey
userID: ID!
workoutID: ID!
variationID: ID!
sets: AWSJSON
}
@rinorocks8 it seems that the warning is a bit of a red herring. During reproduction of this issue, it seems that the first update succeeds without the userID field but other mutations are sending a version number lower than the server version that was incremented on the first successful mutation.
We will investigate this further now that we can consistently reproduce it. Thank you!
Can confirm an similar issue happening but it would seem with the primary key.
Given the following model:
type Pulse @model @auth(rules: [{allow: public}]) {
id: ID!
hashKey: ID! @index(sortKeyFields: ["rangeKey"])
rangeKey: ID!
geoJson: String!
geoHash: String!
}
And I attempt to do
const pulse = new Pulse({
hasKey: "ID-X",
rangeKey: "ID-Y",
geoJson: "A",
geoHash: "B"
})
Datastore.save(pulse)
It results in:
[Error: Field id is required]
It's weird as it happened pre-upgrade to v5.1.4 prior version was set to use v5.0.18.
Manually setting the id
attribute resolves the issue however!
This works:
const pulse = new Pulse({
id: "some-uuid-v4",
hasKey: "ID-X",
rangeKey: "ID-Y",
geoJson: "A",
geoHash: "B"
})
Datastore.save(pulse)
I am having this a similar issue. DataStore is throwing:
Error: Field id is required
at datastore.ts:580:11
at datastore.ts:891:7
at Array.forEach (<anonymous>)
at datastore.ts:888:28
at produce (immerClass.ts:94:14)
at Model.copyOf (datastore.ts:870:18)
at traverseModel (util.ts:220:39)
at StorageAdapterBase2.saveMetadata (StorageAdapterBase.ts:186:27)
at IndexedDBAdapter2.<anonymous> (IndexedDBAdapter.ts:218:9)
at step (tslib.es6.js:147:23)
when I run the DataStore.save() operation with a complete (new, not updated) model.
Manually setting the
id
attribute resolves the issue however!
@Jackson3195 I tried setting the ID with:
id: randomUUID().toString()
but got:
Object literal may only specify known properties, and 'id' does not exist in type 'ModelInit<modelName>'.
How do you work around this?
I'd love an update on this bug if possible as it's one we're actively tracking because we can't update our DataStore package version until it's fixed.
@Jackson3195 @charlieforward9
With respect to the Field: id is required
error, there's an open bug in the codegen side (used by CLI) today that prevents DataStore for recognizing id
is the primary key on models with @index
directives that do not provide a name
attribute. You can see the details in amplify-codegen/issues/561.
Using @Jackson3195's Pulse
model as an example:
type Pulse @model @auth(rules: [{allow: public}]) {
id: ID!
hashKey: ID! @index(sortKeyFields: ["rangeKey"])
rangeKey: ID!
geoJson: String!
geoHash: String!
}
DataStore doesn't know that id
is the PK, so it won't generate a value for it. But, it's a required field, so DataStore demands that you supply the value.
The workaround is simply to add a name
attribute. E.g.,
type Pulse @model @auth(rules: [{allow: public}]) {
id: ID!
hashKey: ID! @index(name: 'byHashKeyAndRangekey', sortKeyFields: ["rangeKey"])
rangeKey: ID!
geoJson: String!
geoHash: String!
}
This will cause the metadata for hashKey
supplied to DataStore to also include a name
property, which tells DataStore that it's not the PK. When DataStore tries to determine the PK, it will then default to id
and generate the id
.
@caridavidson The original issue may be different than the Field: id is required
error that others are seeing. Can you post the graphql schema you're using? (Or at least the relevant model and any related models?)
I posted this 2 months ago. I posted what existed at the time and I have since moved on and dropped using data store completely.
Please use the information provided or find another way to reproduce this.
Thanks.
I also gave up on Datastore because of this error and Datastore’s many limitations. I ended up creating my own version of it using dynamodb and vercel edge functions by creating a similar implementation of watermelondb’s push and pull server functions and implementing my own database sync on the front end.
Before opening, please confirm:
Note that this might be related to https://github.com/aws-amplify/amplify-js/issues/10487
JavaScript Framework
Vue
Amplify APIs
DataStore
Amplify Categories
api
Environment information
Describe the bug
I'm using AWS Amplify DataStore to update a model. First, I create the model with some basic data, and that saves fine. Then I update some fields. Say,
first_name
, and then DataStore generates a mutation (below). It returns an error:DataStore - conflict trycatch Error: Field tenant is required
is a WARN to the console, and the following is the payload returned. Note that tenant is not sent in the update. . Should it be an error? Is this a DataStore bug, or mine? How can I bypass this problem? Maybe it's just missing_version
? I think DataStore should be handling that though, right?Expected behavior
When calling
DataStore.save()
with a valid model, I expect it to save, regardless of what is happening in the background, and without having to fiddle with versioning. I also expect that if there are required fields on the model, and they are required in a mutation, then they are sent with that mutation.Reproduction steps
see above...
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response