简体   繁体   中英

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() . Is it even correct?

I think the error is the data type you're using for the id column of your table. In the documentation it shows that you should define your UUID column like this:

...
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.

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(). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM