Sequelize model.sync()正在引发一个错误



我正在尝试创建一个具有sequelize和add的模型,然后使用sync({force:true}(,如图所示,使MySQL在不存在的情况下创建表。然而,它对我大喊大叫,说我在MySQL查询中有一个错误(它应该是由Sequelize生成的(。这是型号:

// admin.model.js
const { Sequelize, DataTypes } = require('sequelize');
const sequelize = require('../db/connection');
const validator = require("validator")
const Admin = sequelize.define('Admin', {
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate(value) {
if (!validator.isEmail(value)) {
throw new Error('Invalid email');
}
},
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate(value) {
if (!value.match(/d/) || !value.match(/[a-zA-Z]/)) {
throw new Error('Password must contain at least one letter and one number');
}
},
private: true, // used by the toJSON plugin
},
firstName: {
type: DataTypes.STRING,
allowNull: false
},
lastName: {
type: DataTypes.STRING,
allowNull: false
},
phone: {
type: DataTypes.NUMBER
},
}, {
freezeTableName: true
}
)
module.exports = Admin;

这就是我试图运行的功能

// sync.js
const Admin = require("../models/admin.model")
const synchronizeTables = async () => {
try {
await Admin.sync({ force: true })
console.log("Admin Table synchronized")
} catch (error) {
console.error(error)
}
}
module.exports = synchronizeTables

然后我在db目录中有一个index.js文件,用于导出连接和同步:

// index.js
const db = require("./connection");
const sync = require("./sync");
module.exports = { db, sync };

然后,我在app.js中导入它们并运行sync

// app.js
const { db, sync } = require('./src/db');
sync()

运行服务器时出现的错误:

Server is up and running on port 8000
Executing (default): SELECT 1+1 AS result
Executing (default): DROP TABLE IF EXISTS `Admin`;
Connection has been established successfully.
Executing (default): CREATE TABLE IF NOT EXISTS `Admin` (`id` INTEGER NOT NULL auto_increment , `email` VARCHAR(255) NOT NULL UNIQUE, `password` VARCHAR(255) NOT NULL, `firstName` VARCHAR(255) NOT NULL, `lastName` VARCHAR(255) NOT NULL, `phone` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
DatabaseError [SequelizeDatabaseError]: 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 'NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KE' at line 1
at Query.formatError (/home/ikdem/work/upwork/saas/admin-side/node_modules/sequelize/lib/dialects/mysql/query.js:265:16)
at Query.run (/home/ikdem/work/upwork/saas/admin-side/node_modules/sequelize/lib/dialects/mysql/query.js:77:18)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
parent: Error: 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 'NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KE' at line 1
at Packet.asError (/home/ikdem/work/upwork/saas/admin-side/node_modules/mysql2/lib/packets/packet.js:712:17)
at Query.execute (/home/ikdem/work/upwork/saas/admin-side/node_modules/mysql2/lib/commands/command.js:28:26)
at Connection.handlePacket (/home/ikdem/work/upwork/saas/admin-side/node_modules/mysql2/lib/connection.js:425:32)
at PacketParser.onPacket (/home/ikdem/work/upwork/saas/admin-side/node_modules/mysql2/lib/connection.js:75:12)
at PacketParser.executeStart (/home/ikdem/work/upwork/saas/admin-side/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/home/ikdem/work/upwork/saas/admin-side/node_modules/mysql2/lib/connection.js:82:25)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10) {
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 'NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KE' at line 1",
sql: 'CREATE TABLE IF NOT EXISTS `Admin` (`id` INTEGER NOT NULL auto_increment , `email` VARCHAR(255) NOT NULL UNIQUE, `password` VARCHAR(255) NOT NULL, `firstName` VARCHAR(255) NOT NULL, `lastName` VARCHAR(255) NOT NULL, `phone` NUMBER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;',
parameters: undefined
},

因此,Sequelize生成的SQL查询中似乎存在错误。也许我用得不对。请帮忙:(

所以这里的问题是电话的Data类型。正如您所看到的DataType。NUMBER在查询中没有变为INTEGER,而是保留为NUMBER,而NUMBER不存在于SQL中。所以,我不得不改变

phone: {
type: DataTypes.NUMBER
},

phone: {
type: DataTypes.INTEGER
},

此外,我使用sequelize.sync((一次同步所有表,而不是逐个同步模型。

相关内容

最新更新