Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError while generating modelgen in dart #880

Open
silentlie opened this issue Sep 18, 2024 · 1 comment · May be fixed by #887
Open

TypeError while generating modelgen in dart #880

silentlie opened this issue Sep 18, 2024 · 1 comment · May be fixed by #887
Labels
bug Something isn't working Gen 2

Comments

@silentlie
Copy link

Environment information

System:
  OS: Windows 10 10.0.19045
  CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
  Memory: 4.20 GB / 15.92 GB
Binaries:
  Node: 22.0.0 - C:\Program Files\nodejs\node.EXE       
  Yarn: undefined - undefined
  npm: 10.8.0 - C:\Program Files\nodejs\npm.CMD
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.0
  @aws-amplify/backend: 1.1.1
  @aws-amplify/backend-auth: 1.1.3
  @aws-amplify/backend-cli: 1.2.5
  @aws-amplify/backend-data: 1.1.2
  @aws-amplify/backend-deployer: 1.1.0
  @aws-amplify/backend-function: 1.3.3
  @aws-amplify/backend-output-schemas: 1.2.0
  @aws-amplify/backend-output-storage: 1.1.1
  @aws-amplify/backend-secret: 1.1.0
  @aws-amplify/backend-storage: 1.1.2
  @aws-amplify/cli-core: 1.1.2
  @aws-amplify/client-config: 1.2.1
  @aws-amplify/deployed-backend-client: 1.4.0
  @aws-amplify/form-generator: 1.0.1
  @aws-amplify/model-generator: 1.0.5
  @aws-amplify/platform-core: 1.0.6
  @aws-amplify/plugin-types: 1.2.1
  @aws-amplify/sandbox: 1.2.0
  @aws-amplify/schema-generator: 1.2.1
  aws-amplify: 6.5.2
  aws-cdk: 2.154.1
  aws-cdk-lib: 2.154.1
  typescript: 5.5.4
AWS environment variables:
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
  AWS_STS_REGIONAL_ENDPOINTS = regional
No CDK environment variable

Codegen packages

[email protected] G:\ADSATS\adsats_amplify_gen_2
└─┬ @aws-amplify/[email protected]
  └─┬ @aws-amplify/[email protected]
    └── @aws-amplify/[email protected]

Description

Describe the bug:

A model that has composite identifier will cause TypeError: Cannot read properties of undefined (reading 'primaryKeyInfo') while generating graphql model client code

TypeError: Cannot read properties of undefined (reading 'primaryKeyInfo')
[DEBUG] 2024-09-18T10:20:44.432Z: TypeError: Cannot read properties of undefined (reading 'primaryKeyInfo')
    at AppSyncModelDartVisitor.getModelIdentifierFields (G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\appsync-modelgen-plugin\lib\visitors\appsync-dart-visitor.js:874:51)
    at AppSyncModelDartVisitor.generateGetters (G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\appsync-modelgen-plugin\lib\visitors\appsync-dart-visitor.js:268:47)
    at AppSyncModelDartVisitor.generateModelClass (G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\appsync-modelgen-plugin\lib\visitors\appsync-dart-visitor.js:168:14)
    at G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\appsync-modelgen-plugin\lib\visitors\appsync-dart-visitor.js:116:43
    at Array.forEach (<anonymous>)
    at AppSyncModelDartVisitor.generateModelClasses (G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\appsync-modelgen-plugin\lib\visitors\appsync-dart-visitor.js:115:50)
    at AppSyncModelDartVisitor.generate (G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\appsync-modelgen-plugin\lib\visitors\appsync-dart-visitor.js:31:21)
    at Object.plugin (G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\appsync-modelgen-plugin\lib\plugin.js:55:24)
    at G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\graphql-generator\node_modules\@graphql-codegen\core\cjs\execute-plugin.js:36:54
    at G:\ADSATS\adsats_amplify_gen_2\node_modules\@aws-amplify\graphql-generator\node_modules\@graphql-codegen\core\node_modules\@graphql-codegen\plugin-helpers\cjs\profiler.js:7:49

How can this bug be reproduced?

  1. Create two models and a many-to-many model in between
  2. Specify the identifiers by those two Ids
  3. Run npx ampx generate graphql-client-code --format modelgen --model-target dart --out lib/models --branch dev --debug

Please include any code snippets or screenshots that you think would be helpful!

NoticeStaff: a
  .model({
    read_at: a.datetime(),
    noticeId: a.id().required(),
    staffId: a.id().required(),
    notice: a.belongsTo("Notice", "noticeId"),
    staff: a.belongsTo("Staff", "staffId"),
  })
  .identifier(["noticeId", "staffId"]),

npx ampx generate graphql-client-code --format modelgen --model-target dart --out lib/models --branch dev --debug

@silentlie silentlie added the pending-triage Issues that need further discussion to determine label Sep 18, 2024
@dpilch
Copy link
Member

dpilch commented Oct 2, 2024

I have found a solution in #887. We are removing the foreign keys on the belongsTo model for native platforms (dart, java, swift). This is done to improve the developer experience, but it does present an issue when the foreign key is also used as part of the primary key.

We will need to do some testing with the native platforms to ensure the proposed fix does not cause other issues. For now, you can work around this issue by using a different field for your primary key.

@dpilch dpilch added bug Something isn't working and removed pending-triage Issues that need further discussion to determine labels Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Gen 2
Projects
None yet
3 participants