Closed irisk29 closed 2 years ago
Hello @irisk29 - Thanks for opening the issue. A few questions:
Hello @Jordan-Nelson, I believe the problem was caused by the relation of the product to the shoppingBag. Only when I had a shopping bag connected to the product, I could save it. The question is why? I defined this field(the shopping bag id) as not required, to simulate 0..1 relation with the product (product can have 0 or 1 shopping bag) but still could not save the product without the shopping bag ID.
Only when I had a shopping bag connected to the product, I could save it
What was happening when you attempted to save without a shopping bag connected to the product? Was there an exception thrown?
What was happening when you attempted to save without a shopping bag connected to the product? Was there an exception thrown?
No exception was thrown, nothing happened. The state of the information in the cloud was like in the pictures attached.
Only when I had a shopping bag connected to the product, I could save it.
Do you mind explaining what you mean by this? Were you able to get this to work by changing either your schema, or the code being run?
Do you mind explaining what you mean by this? Were you able to get this to work by changing either your schema, or the code being run?
I had to change the code being run. I needed to create the ShoppingBagModel, save it, and then connect the ID of it to the product of the store I wanted to create. Even though, my intention was to be able to create a product for a store without necessarily creating a shopping bag for the product (for which it needs to belong).
@irisk29 - The schema that you shared only has ProductModel
and OnlineStoreModel
. Can you share the schema for ShoppingBagModel
as well?
@irisk29 - The schema that you shared only has
ProductModel
andOnlineStoreModel
. Can you share the schema forShoppingBagModel
as well?
type ShoppingBagModel @model @auth(rules: [{allow: public}]) {
id: ID!
productsAndQuantity: AWSJSON
usermodelID: ID @index(name: "byUserModel")
productModel: [ProductModel] @hasMany(indexName: "byShoppingBagModel", fields: ["id"])
onlineStoreModel: OnlineStoreModel @hasOne
}
@HuiSF - This looks similar to https://github.com/aws-amplify/amplify-flutter/issues/1451. Does this look like the same issue to you?
Thanks @Jordan-Nelson for the initial triaging. Hello @irisk29 for reporting this issue. This is actually a known issue happening in the AppSync transformer:
When a field is used as a Global Secondary Index -
shoppingbagmodelID
is not actually associating the Bag
model using @belongsTo
)The underlying GraphQL mutation will be rejected by DynamoDB as this field will be assigned a null
value by default. Amplify CLI maintainers is currently looking into this issue as https://github.com/aws-amplify/amplify-cli/issues/9915
There is no convenient workaround now unfortunately to 100% replicate your current model structure. (I believe you created the models using Amplify Studio data modeling interface?)
Here's what I do to get around of this personally.
amplify push
to deploy the schematype ProductModel @model @auth(rules: [{allow: public}]) {
id: ID!
name: String
categories: AWSJSON
price: Float
imageUrl: String
description: String
onlinestoremodelID: ID @index(name: "byOnlineStoreModel")
onlineStore: OnlineStoreModel @belongsTo(fields: ["onlinestoremodelID"])
shoppingbagmodelID: ID @index(name: "byShoppingBagModel")
shoppingBag: ShoppingBagModel @belongsTo(fields: ["shoppingbagmodelID"])
}
onlinestoremodelID
and shoppingbagmodelID
fields.var storeModel = ...
var bagModel = ...
// note that we assign to the model field instead of the connection id fields
var product = ProductModel(name: 'A product', onlineStore: storeModel, shoppingBag: bagModel);
await Amplify.DataStore.save(product);
Please note that currently Amplify Studio Data modeling interface doesn't support @belongsTo
directive. So you were planning adapt this workaround, you would need to stay with Amplify CLI dev cycle until Amplify Studio supports it.
I'm going to close this issue in favor of #306 . Please feel to follow up if you have any other question.
Description
Hello, I'm trying to save via Data Store Product that is connected to Store (one Store has many Products). The store is saved successfully (I can see it in the content section in AWS amplify studio) but not the product. I have tried in many ways -
but nothing works, I could never see the product in the content section.
Categories
Steps to Reproduce
the schema is:
This is the code that I'm performing:
Screenshots
Platforms
Environment
Dependencies
Device
iPhone 13
OS
iOS 15.0.0
CLI Version
7.6.22
Additional Context
No response