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.4k stars 2.11k forks source link

list() Command Returns Empty Array on PostgreSQL Auto-Generated Types #13519

Open TazChoudhury opened 2 weeks ago

TazChoudhury commented 2 weeks ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Storage

Amplify Version

v6

Amplify Categories

storage

Backend

Amplify Gen 2 (Preview)

Environment information

``` System: OS: macOS 14.5 CPU: (10) arm64 Apple M1 Pro Memory: 359.33 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 22.2.0 - ~/.nvm/versions/node/v22.2.0/bin/node Yarn: 1.22.22 - ~/.nvm/versions/node/v22.2.0/bin/yarn npm: 10.8.1 - ~/.nvm/versions/node/v22.2.0/bin/npm pnpm: 9.4.0 - ~/.nvm/versions/node/v22.2.0/bin/pnpm Browsers: Chrome: 126.0.6478.62 Edge: 126.0.2592.61 Safari: 17.5 npmPackages: %name%: 0.1.0 @aws-amplify/backend: ^1.0.0 => 1.0.0 @aws-amplify/backend-cli: ^1.0.1 => 1.0.1 @aws-amplify/ui-react: ^6.1.9 => 6.1.9 @aws-amplify/ui-react-internal: undefined () @types/react: ^18.2.66 => 18.3.1 @types/react-dom: ^18.2.22 => 18.3.0 @typescript-eslint/eslint-plugin: ^7.2.0 => 7.8.0 @typescript-eslint/parser: ^7.2.0 => 7.8.0 @vitejs/plugin-react: ^4.2.1 => 4.2.1 aws-amplify: ^6.2.0 => 6.2.0 aws-amplify/adapter-core: undefined () aws-amplify/analytics: undefined () aws-amplify/analytics/kinesis: undefined () aws-amplify/analytics/kinesis-firehose: undefined () aws-amplify/analytics/personalize: undefined () aws-amplify/analytics/pinpoint: undefined () aws-amplify/api: undefined () aws-amplify/api/server: undefined () aws-amplify/auth: undefined () aws-amplify/auth/cognito: undefined () aws-amplify/auth/cognito/server: undefined () aws-amplify/auth/enable-oauth-listener: undefined () aws-amplify/auth/server: undefined () aws-amplify/data: undefined () aws-amplify/data/server: undefined () aws-amplify/datastore: undefined () aws-amplify/in-app-messaging: undefined () aws-amplify/in-app-messaging/pinpoint: undefined () aws-amplify/push-notifications: undefined () aws-amplify/push-notifications/pinpoint: undefined () aws-amplify/storage: undefined () aws-amplify/storage/s3: undefined () aws-amplify/storage/s3/server: undefined () aws-amplify/storage/server: undefined () aws-amplify/utils: undefined () aws-cdk: ^2.138.0 => 2.140.0 aws-cdk-lib: ^2.138.0 => 2.140.0 constructs: ^10.3.0 => 10.3.0 esbuild: ^0.20.2 => 0.20.2 eslint: ^8.57.0 => 8.57.0 eslint-plugin-react-hooks: ^4.6.0 => 4.6.2 eslint-plugin-react-refresh: ^0.4.6 => 0.4.6 react: ^18.2.0 => 18.3.1 react-dom: ^18.2.0 => 18.3.1 rsuite: ^5.64.1 => 5.64.1 rsuite/Accordion: undefined () rsuite/AccordionPanel: undefined () rsuite/Affix: undefined () rsuite/Animation: undefined () rsuite/AutoComplete: undefined () rsuite/Avatar: undefined () rsuite/AvatarGroup: undefined () rsuite/Badge: undefined () rsuite/Bounce: undefined () rsuite/Breadcrumb: undefined () rsuite/BreadcrumbItem: undefined () rsuite/Button: undefined () rsuite/ButtonGroup: undefined () rsuite/ButtonToolbar: undefined () rsuite/Calendar: undefined () rsuite/Carousel: undefined () rsuite/CascadeTree: undefined () rsuite/Cascader: undefined () rsuite/CheckPicker: undefined () rsuite/CheckTree: undefined () rsuite/CheckTreePicker: undefined () rsuite/Checkbox: undefined () rsuite/CheckboxGroup: undefined () rsuite/Col: undefined () rsuite/Collapse: undefined () rsuite/Container: undefined () rsuite/Content: undefined () rsuite/CustomProvider: undefined () rsuite/DOMHelper: undefined () rsuite/DateInput: undefined () rsuite/DatePicker: undefined () rsuite/DateRangeInput: undefined () rsuite/DateRangePicker: undefined () rsuite/Divider: undefined () rsuite/Drawer: undefined () rsuite/DrawerActions: undefined () rsuite/DrawerBody: undefined () rsuite/DrawerFooter: undefined () rsuite/DrawerHeader: undefined () rsuite/DrawerTitle: undefined () rsuite/Dropdown: undefined () rsuite/DropdownItem: undefined () rsuite/DropdownMenu: undefined () rsuite/DropdownSeparator: undefined () rsuite/Fade: undefined () rsuite/FlexboxGrid: undefined () rsuite/FlexboxGridItem: undefined () rsuite/Footer: undefined () rsuite/Form: undefined () rsuite/FormControl: undefined () rsuite/FormControlLabel: undefined () rsuite/FormErrorMessage: undefined () rsuite/FormGroup: undefined () rsuite/FormHelpText: undefined () rsuite/Grid: undefined () rsuite/Header: undefined () rsuite/Heading: undefined () rsuite/HeadingGroup: undefined () rsuite/Highlight: undefined () rsuite/IconButton: undefined () rsuite/InlineEdit: undefined () rsuite/Input: undefined () rsuite/InputGroup: undefined () rsuite/InputGroupAddon: undefined () rsuite/InputGroupButton: undefined () rsuite/InputNumber: undefined () rsuite/InputPicker: undefined () rsuite/List: undefined () rsuite/ListItem: undefined () rsuite/Loader: undefined () rsuite/MaskedInput: undefined () rsuite/Message: undefined () rsuite/Modal: undefined () rsuite/ModalBody: undefined () rsuite/ModalDialog: undefined () rsuite/ModalFooter: undefined () rsuite/ModalHeader: undefined () rsuite/ModalTitle: undefined () rsuite/MultiCascadeTree: undefined () rsuite/MultiCascader: undefined () rsuite/Nav: undefined () rsuite/NavItem: undefined () rsuite/NavMenu: undefined () rsuite/Navbar: undefined () rsuite/NavbarBrand: undefined () rsuite/Notification: undefined () rsuite/Pagination: undefined () rsuite/Panel: undefined () rsuite/PanelGroup: undefined () rsuite/Placeholder: undefined () rsuite/PlaceholderGraph: undefined () rsuite/PlaceholderGrid: undefined () rsuite/PlaceholderParagraph: undefined () rsuite/Popover: undefined () rsuite/Progress: undefined () rsuite/ProgressCircle: undefined () rsuite/ProgressLine: undefined () rsuite/Radio: undefined () rsuite/RadioGroup: undefined () rsuite/RadioTile: undefined () rsuite/RadioTileGroup: undefined () rsuite/RangeSlider: undefined () rsuite/Rate: undefined () rsuite/Row: undefined () rsuite/SafeAnchor: undefined () rsuite/Schema: undefined () rsuite/SelectPicker: undefined () rsuite/Sidebar: undefined () rsuite/Sidenav: undefined () rsuite/SidenavBody: undefined () rsuite/SidenavHeader: undefined () rsuite/SidenavToggle: undefined () rsuite/Slide: undefined () rsuite/Slider: undefined () rsuite/Stack: undefined () rsuite/StackItem: undefined () rsuite/StepItem: undefined () rsuite/Steps: undefined () rsuite/Tab: undefined () rsuite/Table: undefined () rsuite/TableCell: undefined () rsuite/TableColumn: undefined () rsuite/TableColumnGroup: undefined () rsuite/TableHeaderCell: undefined () rsuite/Tabs: undefined () rsuite/Tag: undefined () rsuite/TagGroup: undefined () rsuite/TagInput: undefined () rsuite/TagPicker: undefined () rsuite/Text: undefined () rsuite/Timeline: undefined () rsuite/TimelineItem: undefined () rsuite/Toggle: undefined () rsuite/Tooltip: undefined () rsuite/Transition: undefined () rsuite/Tree: undefined () rsuite/TreePicker: undefined () rsuite/Uploader: undefined () rsuite/VisuallyHidden: undefined () rsuite/Whisper: undefined () rsuite/locales: undefined () rsuite/locales/ar_EG: undefined () rsuite/locales/cs_CZ: undefined () rsuite/locales/da_DK: undefined () rsuite/locales/de_DE: undefined () rsuite/locales/default: undefined () rsuite/locales/en_GB: undefined () rsuite/locales/en_US: undefined () rsuite/locales/es_AR: undefined () rsuite/locales/es_ES: undefined () rsuite/locales/fa_IR: undefined () rsuite/locales/fi_FI: undefined () rsuite/locales/fr_FR: undefined () rsuite/locales/hu_HU: undefined () rsuite/locales/it_IT: undefined () rsuite/locales/ja_JP: undefined () rsuite/locales/kk_KZ: undefined () rsuite/locales/ko_KR: undefined () rsuite/locales/ne_NP: undefined () rsuite/locales/nl_NL: undefined () rsuite/locales/pt_BR: undefined () rsuite/locales/ru_RU: undefined () rsuite/locales/sv_SE: undefined () rsuite/locales/tr_TR: undefined () rsuite/locales/zh_CN: undefined () rsuite/locales/zh_TW: undefined () rsuite/toaster: undefined () rsuite/useBreakpointValue: undefined () rsuite/useMediaQuery: undefined () rsuite/useToaster: undefined () sass: ^1.77.6 => 1.77.6 tsx: ^4.7.2 => 4.9.1 typescript: ^5.4.5 => 5.4.5 (4.4.4, 4.9.5) vite: ^5.2.0 => 5.2.11 npmGlobalPackages: @aws-amplify/cli: 12.12.2 corepack: 0.28.2 ncu: 0.2.1 npm-check-updates: 16.14.20 npm: 10.8.1 ```

Describe the bug

Having run the command npx ampx generate schema-from-database --connection-uri-secret SQL_CONNECTION_STRING --out amplify/data/schema.sql.ts, I have the schema.sql.ts file generated.

The backend deploys successfully, and the create, update, and delete commands work as expected. However the list command always returns an empty array for models where the identifier is a type that is a.string().required().

Below model from schema.sql.ts doesn't work for list command:

"auth": a.model({ user_id: a.string().required(), username: a.string().required(), password: a.string().required(), access: a.boolean(), created: a.datetime(), updated: a.datetime() }).identifier([ "user_id" ]),

It works when the model has an identifier of type a.integer().required(). This model works for the list command:

"one_off": a.model({ id: a.integer().required(), token: a.string(), amount: a.integer(), used: a.integer(), expiry_date: a.datetime(), created: a.datetime(), updated: a.datetime() }).identifier([ "id" ]),

This is potentially because the graphql request for listAuth from the client.models.auth.list() function call is:

"query ($user_id: String, $sortDirection: ModelSortDirection, $filter: ModelAuthFilterInput, $limit: Int, $nextToken: String) {\n listAuths(\n user_id: $user_id\n sortDirection: $sortDirection\n filter: $filter\n limit: $limit\n nextToken: $nextToken\n ) {\n items {\n user_id\n username\n password\n access\n created\n updated\n }\n nextToken\n __typename\n }\n}\n"

This adds the identifier into the query as $user_id: String but this is empty and then won't return any data that matches.

The graphql for the listOne_offs is:

query ($filter: ModelOne_offFilterInput, $limit: Int, $nextToken: String) { listOne_offs(filter: $filter, limit: $limit, nextToken: $nextToken) { items { id token amount used expiry_date created updated } nextToken __typename } }

Id isn't included and so all the data is returned from the list command.

I know the connection has been made to the database due to the other commands working and I can also see the data in the Data Manager view.

Expected behavior

The expected behaviour is that the list command works with generated models with string types as their id.

Reproduction steps

  1. Perform the steps from this guide
  2. Have a table with a string type as a primary key in a postgresql database.
  3. Ensure there is data in your table.
  4. Utilise the const result = await client.models.[type].list(); in the app.
  5. Try and use the result in a part of your app to see the results.
chrisbonifacio commented 1 week ago

Hi @TazChoudhury thank you for raising this issue and providing a detailed description and reproduction steps.

We will attempt to reproduce the issue and report back soon

chrisbonifacio commented 1 week ago

Hi @TazChoudhury 👋 apologies for the delay. I was not able to able to reproduce this issue on the latest versions of @aws-amplify/backend and @aws-amplify/backend-cli packages, with the following schema.sql.ts:

.schema({
    "locations": a.model({
        id: a.string().required(),
        name: a.string().required(),
        geom: a.string().required(),
        address: a.string()
    }).identifier([
        "id"
    ]),
    "posts": a.model({
        id: a.string().required(),
        title: a.string().required(),
        published: a.boolean(),
        owner: a.string()
    }).identifier([
        "id"
    ]),

graphql query:

query ($filter: ModelPostsFilterInput, $limit: Int, $nextToken: String) {  
  listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {   
    items {      
      id      
      title      
      published      
      owner    
    }    
    nextToken    
    __typename  
}

client request:

const { data } = await client.models.posts.list();

console.log(data);

Data manager:

CleanShot 2024-06-27 at 07 13 49

Client response:

CleanShot 2024-06-27 at 07 14 46

chrisbonifacio commented 1 week ago

@TazChoudhury I would recommend upgrading your backend and backend-cli packages to the latest version and see if that helps resolve the issue.

npm i @aws-amplify/backend @aws-amplify/backend-cli

Afterwards, run this to update the @aws-amplify/data-schema package as well

npm update @aws-amplify/data-schema