[英]Cant figure out what is wrong with my models
I set up my db like:我设置了我的数据库:
const Sequelize = require("sequelize");
const db = {};
const sequelize = new Sequelize("sequel", "root", "root", {
host: "localhost",
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
logging: false
});
db.Sequelize = Sequelize;
db.sequelize = sequelize;
module.exports = db;
And my user model like this:我的用户模型是这样的:
const { Sequelize, sequelize } = require("../config/database");
const user = sequelize.define("users", {
id: {
type: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
email: {
type: Sequelize.STRING,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
},
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
}
});
user.sync();
module.exports = user;
When I run server my error is this:当我运行服务器时,我的错误是这样的:
Unhandled rejection SequelizeDatabaseError: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'UUIDV4 NOT NULL , `email` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NUL' at line 1
at Query.formatError (C:\Users\HP\Documents\sum\chiya\node_modules\sequelize\lib\dialects\mysql\query.js:239:16)
at Query.handler [as onResult] (C:\Users\HP\Documents\sum\chiya\node_modules\sequelize\lib\dialects\mysql\query.js:46:23)
at Query.execute (C:\Users\HP\Documents\sum\chiya\node_modules\mysql2\lib\commands\command.js:30:14)
at Connection.handlePacket (C:\Users\HP\Documents\sum\chiya\node_modules\mysql2\lib\connection.js:449:32)
at PacketParser.onPacket (C:\Users\HP\Documents\sum\chiya\node_modules\mysql2\lib\connection.js:72:12)
at PacketParser.executeStart (C:\Users\HP\Documents\sum\chiya\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (C:\Users\HP\Documents\sum\chiya\node_modules\mysql2\lib\connection.js:79:25)
at Socket.emit (events.js:200:13)
at addChunk (_stream_readable.js:290:12)
at readableAddChunk (_stream_readable.js:271:11)
at Socket.Readable.push (_stream_readable.js:226:10)
at TCP.onStreamRead (internal/stream_base_commons.js:166:17)
As I am new to this what am I doing wrong to cause this syntax error??因为我是新手,我做错了什么导致这个语法错误??
And one extra thing if there is no table create one.如果没有表创建一个额外的事情。 I researched it but couldn't find anything that clearly so I used
user.sync()
.我研究了它,但找不到任何清楚的东西,所以我使用了
user.sync()
。 Is it even correct?它甚至正确吗?
I think the error is the data type you're using for the id column of your table.我认为错误是您用于表的 id 列的数据类型。 In the documentation it shows that you should define your UUID column like this:
在文档中,它显示您应该像这样定义 UUID 列:
...
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
...
UUIDV4 is just a default unique identifier generated using the UUID v4 standard and should not be used as a data type. UUIDV4 只是使用 UUID v4 标准生成的默认唯一标识符,不应用作数据类型。
And one extra thing if there is no table create one.
如果没有表创建一个额外的事情。 I researched it but couldn't find anything that clearly so I used user.sync().
我研究了它,但找不到任何清楚的东西,所以我使用了 user.sync()。 Is it even correct?
它甚至正确吗?
As the documentation reports here , it is correct.正如此处的文档所报告的那样,它是正确的。 The sync method is used to create the table if it doesn't exist on the database.
如果数据库中不存在该表,则同步方法用于创建该表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.