Open Lorenzohidalgo opened 2 years ago
Hi @Lorenzohidalgo - Can you include the portion of your schema that defines TokenCategory
?
The variable partner is present but not required
It looks like it is not required in the schema. If you want to make it required, you can update:
partner: NPartner @belongsTo(fields: ["partnerPID"])
to:
partner: NPartner! @belongsTo(fields: ["partnerPID"])
Hi @Jordan-Nelson,
the schema for TokenCategory
is:
type TokenCategory @model @auth(rules: [{allow: private}]) {
id: ID!
pid: ID
partnerPID: ID! @index(name: "tokenCategoryByPartnerPID", queryField: "tokenCategoryByPartnerPID")
tokenPackPID: ID! @index(name: "tokenCategoryByPackPID", queryField: "tokenCategoryByPackPID")
tokens: [NToken] @hasMany(indexName: "ntokenByCategoryPID", fields: ["id"])
dbIcon: AWSJSON!
name: AWSJSON!
description: AWSJSON!
action: AWSJSON
valueType: ValueTypeEnum!
color: String!
}
The issue here is that partnerPID
is not present. I want partner
to be nullable. Sorry if I didn't express it correctly.
What I meant by:
- The variable partner is present but not required.
- Therefore there is no "reliable" way of getting the id of the partner
Is that with the currently generated model, I could end up creating an instance of NTokenPack
with the missing partnerPID
(which is required).
I'm also unable to access the partnerPID
variable if I don't query for the partner
also.
The "fix" or expected behavior would be to include the partnerPID
variable in the generated model. This is also the behaviour if I remove the @belongsTo(fields: ["partnerPID"])
statement.
Hi @Lorenzohidalgo
schema.graphql
if you want it to be required in the generated model. You've already done that for other fields with the !
. NTokenPack
without a partner
. Once you've made the partner
field required and you've created a NTokenPack
with a partner
, try querying for your NTokenPack
. In the resulting model returned, the partner
field should be filled. The fields of this partner
field will be null but its id field will not be. That is the equivalent of the partnerPID
variable you are trying to access.
Hi @fjnoyp,
This doesn't solve the issue. I don't want to query for the partner
every single time. I just need the generated model to have the partnerPID
variable available.
Generating the model without the @belongsTo
relationship does generate the model with the partnerPID
variable.
Adding partner: NPartner @belongsTo(fields: ["partnerPID"])
to the model should only add the partner
variable and not remove the partnerPID
one.
Hi @Lorenzohidalgo - Sorry for the delayed response.
I am not sure why the @belongsTo
directive removes the ID field. I can see if this is intentional, or possibly an oversight. As a work around, you may be able to make some updates to your schema to use the @hasOne
directive. I believe the ID field would still be present with a schema such as the one below.
You can find more info about the hasOne directive in the docs here: https://docs.amplify.aws/cli/graphql/data-modeling/#has-one-relationship
type Parent @model {
id: ID!
name: String
childID: ID
child: Child @hasOne(fields: ["childID"])
}
type Child @model {
id: ID!
name: String
}
Hi @Jordan-Nelson,
The Docs state:
The @hasOne and @hasMany directives do not support referencing a model which then references the initial model via @hasOne or @hasMany if DataStore is enabled.
So I understand that the proposed workaround won't work.
@Lorenzohidalgo - Ah, I missed that this was a hasMany relationship. Apologies.
@Lorenzohidalgo - I had a missed a few things in your original issue description. Some notes below.
The variable partnerPID is not present The variable partner is present but not required
If you have fetched nTokenPack
via Amplify.DataStore.query(), you should be able to access this via nTokenPack.partner!.id
. The parent (partner) should always be eagerly loaded in a hasMany <-> belongsTo relationship, meaning that if nTokenPack
is fetched with DataStore, it should include partner
.
Can you confirm that partner
is present when nTokenPack
is fetched via datastore operations, and that the partner ID can be accessed via nTokenPack.partner!.id
?
In older versions of DataStore, the associated model was not automatically loaded. partner
would have had to be fetched manually. I believe this is the reason why partner is present but not required in the generated model. Having to cast this is definitely a sub-optimal developer experience though.
We have an issue open to track developer experience improvements around associated models (https://github.com/aws-amplify/amplify-flutter/issues/1449). The improvements being tracked by that issue will likely cover this. The generated model could potentially be updated to make associated model non-null until as a short term fix until 1449 is picked up. I think we can keep this issue open as a separate request to look into a short term to improvement.
The parent (partner) should always be eagerly loaded in a hasMany <-> belongsTo relationship
I wanted to add a clarification here, the parent will always be loaded, assuming the parent exists. So, unless you are sure the parent exists, I would access the parent ID via nTokenPack.partner?.id
.
If the parent ID were added to the model, it would have to be optional since the relationship is not required in the schema. So I think nTokenPack.partner?.id
would be a sufficient replacement for what you were originally looking for - nTokenPack.partnerID
.
I would have to confirm, but I think partner
would be non-null if the schema was updated to be required, but it sounds like you do not want it to be required.
Does using nTokenPack.partner?.id
work for your use case?
I see you are using API, not DataStore. I had thought you were using DataStore. The schema and generated models are used for both. Most of what I said above was specific to DataStore.
Sorry for the confusion. I was having a hard time understanding what you are trying to do since I thought this was for DataStore.
I'm going to mark this as a feature request since there isn't currently a way access the nested model ID without creating the full nested model as you had stated.
Jordan-Nelson.Still waiting for your update.
Hello @TheWhiteShade - I don't have an update on this feature request at the moment.
Anyone have any workarounds for this? Why is the id field missing?
Description
The automatically generated Data Models are missing the relationship IDs.
schema.graphql example:
As defined by the schema, the NTokenPack model has the required field partnerPID.
The generated model has only the following variables available:
This presents the following issues:
partnerPID
is not present.partner
is present but not required.id
of thepartner
Since it's not a one-to-one relationship, I won't be needing to query for the related partner every time. I would like to be able to access the
partnerPID
variable without having to declare/query for the whole partner model each time.Categories
Steps to Reproduce
Screenshots
No response
Platforms
Android Device/Emulator API Level
No response
Environment
Dependencies
Device
N/A
OS
N/A
CLI Version
8.0.2
Additional Context
No response