简体   繁体   English

无法弄清楚我的模型有什么问题

[英]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.

相关问题 node-postgres:我无法弄清楚我的查询有什么问题? (错误:“a”处或附近的语法错误) - node-postgres: I can't figure out what's wrong with my query? (ERROR: syntax error at or near “a”) 我似乎无法弄清文件夹结构,它说系统找不到我的文件? - I cant seem to figure out folder structure and it says the system cant find my file? NodeJs:有没有办法弄清楚我的程序没有使用哪些模块? - NodeJs: Is there a way to figure out what modules are not being used by my program? 我不知道为什么我的提示不起作用。 interface.prompt? - I cant figure out why my prompt is not working. interface.prompt? 无法弄清楚为什么在使用 nodeJs 创建服务器时我的 CMD 中出现错误 - cant figure out why there is error in my CMD while creating server with nodeJs Node.js 中的“转换错误”是什么? 我不知道出了什么问题 - What is 'cast error' about in Node.js? I can't figure out what's wrong 我需要在短时间内学习 vuex,因为我不知道如何将我的 vue js 迁移到 vuex? - I need to learn vuex in short time because and i cant figure out how to migrate my vue js to vuex? 无法找出我的服务有什么问题 - Cannot find out what's wrong with my service 无法弄清楚如何使用自举表与电子渲染器一起工作 - Cant figure out how to get bootstrap table to work with electron renderer NodeJS-我无法弄清的MongooseJS模式错误 - NodeJS - MongooseJS schema error that i cant figure out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM