我将 node.js 与 express 和 sequelize 一起使用,我的数据库是 mysql。我试图创建一个类模型,如文档中所写:https://sequelize.org/master/manual/model-basics.html。我能够连接到数据库,但无法弄清楚如何将模型与数据库同步以创建表。这是我的用户模型:
const { DataTypes, Model } = require('sequelize');
const connection = require("../server");
export class User extends Model { }
User.init({
id: {
type: DataTypes.BIGINT,
autoIncrement: true,
primaryKey: true,
unique: true,
allowNull: false
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
userType: {
type: DataTypes.STRING,
allowNull: false,
}
}, {
connection
});
这是我的 server.js:
const express = require("express");
const Sequelize = require("sequelize");
const app = express();
const port = 8080;
const connection = new Sequelize("coupons2", "root", "1234", {
host: "localhost",
dialect: "mysql"
})
connection.sync()
.then(() => {
console.log("Connection to DB was successful");
})
.catch(err => {
console.error("Unable to connect to DB", err);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
})
module.exports = connection;
谢谢你!
料青山看我应如是
肥皂起泡泡
阿波罗的战车
相关分类