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

doc(data): support for auto increment (serial) fields #8074

Closed
wants to merge 3 commits into from

Conversation

p5quared
Copy link
Contributor

@p5quared p5quared commented Nov 4, 2024

Description of changes:

We now support modeling SQL generated auto increment (serial) fields.

e.g.

Database model

-- PostgreSQL 
create table example (
  serialPK SERIAL PRIMARY KEY,
  serialNumber SERIAL,
  content text
);

Amplify model

// Simplification - removed boilerplate
a.schema({
    "example": a.model({
        serialPK: a.integer().default(),
        serialNumber: a.integer().default(),
        content: a.string(),
    }).identifier([
        "serialNumber"
    ]),
})

Notes:

  • We only support PostgreSQL
  • Only supported field is SERIAL (aka auto increment)
  • The special syntax a.integer().default() denotes a generated integer on TS schema
  • The special syntax f: Int! @default now exists on GQL schema
  • As far as the schema knows, these fields are nullable hence why .identifier now can accept nullable fields. However, new checks block users from improperly specifying a nullable identifier.

Related GitHub issue #, if available:

aws-amplify/amplify-category-api#2883

Instructions

Pending some release propagations before npx ampx generate-schema-from-database will generate the proper field types for SERIAL fields (a.integer().default()).

If this PR should not be merged upon approval for any reason, please submit as a DRAFT

Which product(s) are affected by this PR (if applicable)?

  • amplify-cli
  • amplify-ui
  • amplify-studio
  • amplify-hosting
  • amplify-libraries

Which platform(s) are affected by this PR (if applicable)?

  • JS
  • Swift
  • Android
  • Flutter
  • React Native

Please add the product(s)/platform(s) affected to the PR title

Checks

  • Does this PR conform to the styleguide?

  • Does this PR include filetypes other than markdown or images? Please add or update unit tests accordingly.

  • Are any files being deleted with this PR? If so, have the needed redirects been created?

  • Are all links in MDX files using the MDX link syntax rather than HTML link syntax?

    ref: MDX: [link](https://docs.amplify.aws/)
    HTML: <a href="https://docs.amplify.aws/">link</a>

When this PR is ready to merge, please check the box below

  • Ready to merge

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@@ -233,6 +233,9 @@ const schema = a.schema({
}),
}).authorization((allow) => allow.publicApiKey());
```

If you're working with a Postgres datasource, `.default()` can be used to represent [serial fields](/[platform]/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/#mapping-of-sql-data-types-to-field-types-for-auto-generated-schema).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to specify that .default() can only be applied to .integer() fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded to make integer fields more explicit

If you're working with a Postgres datasource, .default() can be applied to integer fields to represent serial fields.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does .default() require an argument here similar to the graphql directive?

Copy link
Contributor Author

@p5quared p5quared Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we've removed the required arg for both. .default() maps to @default. There are checks in place so that this special (no argument) case can only be applied if:

  1. Datsource is Postgres
  2. Fieldtype is integer

@p5quared p5quared closed this by deleting the head repository Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants