【问题标题】:how to use Sequelize ORM when an error occurs as SHOW INDEX FROM `user_roles`发生错误时如何使用 Sequelize ORM 作为 SHOW INDEX FROM `user_roles`
【发布时间】:2021-04-11 22:32:39
【问题描述】:

这是我的 server.js 当我运行 node server.js 时出现错误 执行中(默认):SHOW INDEX FROM user_roles

更多代码可以看这里

https://github.com/bezkoder/node-js-jwt-auth

const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");

const app = express();

var corsOptions = {
  origin: "http://localhost:8081"
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(bodyParser.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// database
const db = require("./app/models");
const Role = db.role;

db.sequelize.sync();
// force: true will drop the table if it already exists
// db.sequelize.sync({force: true}).then(() => {
//   console.log('Drop and Resync Database with { force: true }');
//   initial();
// });

// simple route
app.get("/", (req, res) => {
  res.json({ message: "Welcome to bezkoder application." });
});

// routes
require('./app/routes/auth.routes')(app);
require('./app/routes/user.routes')(app);

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

function initial() {
  Role.create({
    id: 1,
    name: "user"
  });
 
  Role.create({
    id: 2,
    name: "moderator"
  });
 
  Role.create({
    id: 3,
    name: "admin"
  });
}

我正在使用 Sequelize 是一个基于 Promise 的 Node.js ORM,用于 Postgres、MySQL、MariaDB、SQLite 和 Microsoft SQL Server。它具有可靠的事务支持、关系、急切和延迟加载、读取复制等。

node --version
v15.5.0



const db = {};

db.Sequelize = Sequelize;
db.sequelize = sequelize;

db.user = require("../models/user.model.js")(sequelize, Sequelize);
db.role = require("../models/role.model.js")(sequelize, Sequelize);

db.role.belongsToMany(db.user, {
  through: "user_roles",
  foreignKey: "roleId",
  otherKey: "userId"
});
db.user.belongsToMany(db.role, {
  through: "user_roles",
  foreignKey: "userId",
  otherKey: "roleId"
});

db.ROLES = ["user", "admin", "moderator"];

module.exports = db;

这是我在终端中得到的输出

(节点:6384)[SEQUELIZE0004] DeprecationWarning:布尔值已传递给 options.operatorsAliases。这是 v5 的无操作,应该被删除。 (使用node --trace-deprecation ... 显示警告的创建位置)
服务器在 8080 端口上运行。
执行(默认): CREATE TABLE IF NOT EXISTS users (id INTEGER NOT NULL auto_increment , username VARCHAR(255), email VARCHAR(255), password VARCHAR(255), createdAtDATE非空,updatedAt 日期时间非空,主键 (id)) ENGINE=InnoDB;
执行中(默认):SHOW INDEX FROM users
执行(默认): CREATE TABLE IF NOT EXISTS roles (id INTEGER , name VARCHAR(255), createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL, PRIMARY KEY (id)) ENGINE =InnoDB;
执行中(默认):SHOW INDEX FROM roles
执行(默认): CREATE TABLE IF NOT EXISTS user_roles (createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL, roleId INTEGER , userId INTEGER , PRIMARY KEY (roleId, @98765434@8)外键 (roleId) 引用 roles (id) 更新级联时删除级联,外键 (userId) 引用users (id) 更新级联时删除级联) 引擎=Inno;
执行中(默认):SHOW INDEX FROM user_roles

然后挂起

【问题讨论】:

  • 我没有看到错误消息本身。您可以将其添加到您的帖子中吗?
  • @Antony 我已添加

标签: node.js sequelize.js


【解决方案1】:

我相信这不是“挂起”问题。这只是 sequelize 的最后一个输出,并且已正确执行。这绝对是一个用户体验问题,因为它“似乎”被冻结了。

尝试并访问该应用程序,它会正常工作。

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 2018-03-27
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2019-06-25
    • 2014-10-07
    • 1970-01-01
    • 2015-06-16
    相关资源
    最近更新 更多