aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
81 stars 71 forks source link

When connecting to mysql, a type error occurs in schema.sql.ts #2596

Open rnrnstar2 opened 3 weeks ago

rnrnstar2 commented 3 weeks ago

Environment information

System:
  OS: macOS 14.0
  CPU: (10) arm64 Apple M2 Pro
  Memory: 163.45 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.5.0 - /usr/local/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 9.8.0 - /usr/local/bin/npm
  pnpm: 8.15.5 - ~/Library/pnpm/pnpm
NPM Packages:
  @aws-amplify/backend: 1.0.3
  @aws-amplify/backend-cli: 1.0.4
  aws-amplify: 6.3.2
  aws-cdk: 2.142.0
  aws-cdk-lib: 2.142.0
  typescript: 5.4.5
AWS environment variables:
  AWS_PROFILE = rnrnstar
  AWS_DEFAULT_PROFILE = rnrnstar
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

An error occurs when an enum is set as a required column.

/* eslint-disable */
/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */
import { a } from "@aws-amplify/data-schema";
import { configure } from "@aws-amplify/data-schema/internals";
import { secret } from "@aws-amplify/backend";

export const schema = configure({
    database: {
        ...
    }
}).schema({
    "Post": a.model({
        id: a.integer().required(),
        name: a.string().required(),
        message: a.string().required(),
        gender: a.enum([
            "SECRET",
            "MEN",
            "WOMEN"
        ]).required(),
        createdAt: a.datetime(),
        updatedAt: a.datetime()
    }).identifier([
        "id"
    ])
});

error message Property 'required' does not exist on type 'EnumType<readonly ["SECRET", "MEN", "WOMEN"]>'. ts(2339)

rnrnstar2 commented 3 weeks ago

When creating data in a connected mysql table, you need to specify an id.

await dataClient.models.Post.create({
  id: 0,
  name: "test",
});

I have it set as id INT AUTO_INCREMENT PRIMARY KEY so it should be set automatically.

Also, when deploying, an error occurs in the id part of the sql file. This seems to be related to the part where the enum is set.

./amplify/data/schema.sql.ts:117:9
Type error: Type '"id"' is not assignable to type '"platform" | "gender" | "prefecture" | "ageGroup"'.
ykethan commented 3 weeks ago

Hey👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂

chrisbonifacio commented 3 weeks ago

Hi @rnrnstar2 , can you confirm that the schema was generated with the enum as required that way or did you manually edit it?

Also, what is the type for that field in your sql database's schema?

rnrnstar2 commented 3 weeks ago

I am using aurora mysql on aws to create a sql database.

gender: a.enum([
"SECRET",
"MEN",
"WOMEN"
]).required(),

Since an error occurs when "required" is included, I changed the table to one that has been deleted. If possible, I would like to add a NOT NULL constraint to the enum as well.

CREATE TABLE Post (
id CHAR(36) PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
categoryId VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
gender ENUM('SECRET', 'MEN', 'WOMEN'),
weekday INT,
hour INT,
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_weekday (weekday, hour, createdAt DESC),
INDEX idx_categoryId (categoryId, createdAt DESC),
);

This is the table I am creating.

When deploying to amplify, an error occurs if the following items are present.

.identifier([
"id"
])

You can deploy by manually deleting these, but this ignores the following advice.

/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */

rnrnstar2 commented 2 weeks ago

Is this issue under investigation?

chrisbonifacio commented 2 weeks ago

Hi @rnrnstar2 Apologies for the delay, thank you for the info!

I will be trying to reproduce this issue today and will report back with any findings