amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

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

Open rnrnstar2 opened this issue 1 year ago • 10 comments

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 avatar Jun 02 '24 07:06 rnrnstar2

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"'.

rnrnstar2 avatar Jun 02 '24 08:06 rnrnstar2

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

ykethan avatar Jun 03 '24 14:06 ykethan

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?

chrisbonifacio avatar Jun 03 '24 18:06 chrisbonifacio

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 avatar Jun 04 '24 04:06 rnrnstar2

Is this issue under investigation?

rnrnstar2 avatar Jun 11 '24 10:06 rnrnstar2

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

I will try to reproduce this issue and will report back with any findings

chrisbonifacio avatar Jun 11 '24 16:06 chrisbonifacio

Hi @rnrnstar2 Apologies for the delay. We've been able to reproduce this issue internally so I'll mark this is as a bug for the team to investigate further.

chrisbonifacio avatar Jul 01 '24 14:07 chrisbonifacio

Note for buildtime team: this appears to be a bug in the generate schema-from-database script.

a.enum does not support the .required modifier. We'll need to generate a top-level enum and set the field to a.ref('EnumName').required() in the script.

iartemiev avatar Jul 22 '24 14:07 iartemiev

Is there any workaround while we wait for the new script version?

lukegorman avatar Oct 08 '24 13:10 lukegorman

@iartemiev That was going to be my approach as well, but then got blocked by https://github.com/aws-amplify/amplify-data/issues/435 when it comes to (custom?) mutations.

hardchor avatar Nov 10 '24 10:11 hardchor