'Typescript and squelize :- You have an error in your SQL syntax; check the manual that corresponds to your MySQL
The error occurs when connecting to mysql in typescript.
The error:
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UUIDV4 NOT NULL auto_increment , `title` VARCHAR(255) NOT NULL, `completed
` TINY' at line 1",
sql: 'CREATE TABLE IF NOT EXISTS `typescript_table` (`id` UUIDV4 NOT NULL auto_increment , `title` VARCHAR(255) NOT NULL, `completed` TINYINT(1) NOT NULL DEFAULT false, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME N
OT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;',
parameters: undefined
},
sql: 'CREATE TABLE IF NOT EXISTS `typescript_table` (`id` UUIDV4 NOT NULL auto_increment , `title` VARCHAR(255) NOT NULL, `completed` TINYINT(1) NOT NULL DEFAULT false, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT
NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;',
parameters: {}
folder structure
- src
|--modal
| |-index.ts
|--config
| |-database.config.ts
app.ts
app.ts
import express,{Request,Response} from "express";
// import {ErrorRequestHandler} from "express";
import HttpError from "http-errors";
import exampleRoute from "./routes/routes";
import db from './config/database.config';
import { v4 as uuidv4 } from 'uuid';
import {CrudInstance} from './modal/index';
const app = express();
const port = 3000;
app.use(express.json());
// app.use(express.urlencoded({extended:true}))
db.sync().then(() => {
console.log('connect to db');
})
// app.get("/api",exampleRoute);
app.post("/create",async(req:Request,res:Response) => {
// console.log("data from postman --->",req.body);
const id =uuidv4();
try {
const record = await CrudInstance.create({...req.body,id})
return res.json({record,msg:"successfully create table"});
} catch(e) {
return res.json({ msg:"fail to create", status:500, route:'/create' });
}
})
app.use(() => {
throw HttpError(404,"Not Found");
});
app.listen(port, ()=> {
console.log(`server up on port no http://localhost:${port}`);
})
// const errorHandler:ErrorRequestHandler = (err,req,res,next) => {
// console.log(err.message , err.statusCode);
// if (res.headersSent) {
// return next(err);
// }
//
// res.status(err.statusCode || 500).json({message:err.message || "An Unknow Error"});
// }
modal/index.ts
import {Sequelize,Model, DataTypes} from "sequelize";
import db from '../config/database.config';
interface crudAttributes {
id: string,
title: string,
completed: boolean
}
export class CrudInstance extends Model<crudAttributes> {}
CrudInstance.init(
{
id: {
type: DataTypes.UUIDV4,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
title:{
type: DataTypes.STRING,
allowNull: false,
},
completed: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
}, {
sequelize:db,
tableName:'typescript_table'
}
);
// CrudInstance.sync();
config/database.config.ts
import { Sequelize } from 'sequelize';
const db = new Sequelize("CRUD","xyz","xyz",{
host: 'localhost',
dialect: "mysql",
logging: false
});
// db.sync();
export default db;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
