amplify-category-api
amplify-category-api copied to clipboard
When connecting to mysql, a type error occurs in schema.sql.ts
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)
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"'.
Hey👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂
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?
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. */
Is this issue under investigation?
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
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.
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.
Is there any workaround while we wait for the new script version?
@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.