aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

Type Error with Custom Identifier in Amplify Data Model Schema - .identifier is not assignable to referenced customType #14000

Closed MATTiVOLTARii closed 3 days ago

MATTiVOLTARii commented 1 week ago

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

DataStore

Amplify Version

v6

Amplify Categories

No response

Backend

Amplify Gen 2

Environment information

``` # Put output below this line System: OS: Windows 11 10.0.22631 CPU: (8) x64 Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz Memory: 11.99 GB / 31.60 GB Binaries: Node: 20.16.0 npm: 9.9.3 npmPackages: @aws-amplify/adapter-nextjs: ^1.2.26 => 1.2.26 @aws-amplify/adapter-nextjs/api: undefined () @aws-amplify/adapter-nextjs/data: undefined () @aws-amplify/backend: ^1.6.0 => 1.6.0 @aws-amplify/backend-cli: ^1.2.2 => 1.2.2 @aws-crypto/sha256-js: ^5.2.0 => 5.2.0 (1.2.2) @aws-sdk/client-cognito-identity-provider: ^3.654.0 => 3.654.0 @aws-sdk/client-s3: ^3.654.0 => 3.654.0 amazon-cognito-identity-js: ^6.1.2 => 6.3.12 aws-amplify: ^6.5.0 => 6.5.0 aws-cdk: ^2.165.0 => 2.165.0 aws-cdk-lib: ^2.151.0 => 2.165.0 aws-sdk: ^2.1691.0 => 2.1691.0 npmGlobalPackages: @aws-amplify/cli: 12.13.0 aws-cdk: 2.165.0 ```

Describe the bug

I’m encountering an error when attempting to define a custom identifier for my model in Amplify’s data schema. Despite using the recommended syntax, the error persists. Below are the details of the setup and the error message.

Expected behavior

The custom identifier customId should be accepted as the primary identifier for the Shop model without type conflicts.

Reproduction steps

Shop: a.model({ customId: a.id().required(), items: a.ref('Items'), content: a.string(), }) .identifier(['customId'])

Code Snippet

import { type ClientSchema, a, defineData } from '@aws-amplify/backend';

const schema = a.schema({ Items: a.customType({ name: a.string(), price: a.string(), }),

Shop: a.model({ customId: a.id().required(), items: a.ref('Items'), content: a.string(), }) .identifier(['customId']) .authorization((allow) => [allow.guest()]), });

export type Schema = ClientSchema;

export const data = defineData({ schema, authorizationModes: { defaultAuthorizationMode: "userPool", }, });

Log output

``` // Put your logs below this line Type error: Type '"customId"' is not assignable to type '"items"'. 13 | items: a.ref('Items'), 14 | content: a.string(), > 15 | }).identifier(['customId']) // Only for models | ^ 16 | .authorization((allow) => [allow.guest()]), ```

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

MATTiVOLTARii commented 1 week ago

I don't know exactly why but with "as any" it seems to work:

  Items: a.customType({
    name: a.string(),
    price: a.string(),
    }),

    Shop: a.model({
    customId: a.id().required() as any,
    items: a.ref('Items'),
    content: a.string(),
    })
    .identifier(['customId'])
    .authorization((allow) => [allow.guest()]),
chrisbonifacio commented 1 week ago

Hi @MATTiVOLTARii 👋 thanks for raising this issue.

can you please share the dependencies listed in your package.json file?

also, curious what version of @aws-amplify/data-schema you have installed. you can check this by running:

npm list @aws-amplify/data-schema

chrisbonifacio commented 3 days ago

Hi 👋 Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.

Thank you!