sivarajan g
sivarajan g

Reputation: 1

how to use Sequelize ORM when an error occurs as SHOW INDEX FROM `user_roles`

This is my server.js When i run node server.js I am getting error as Executing (default): SHOW INDEX FROM user_roles

The more code can be seen here

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"
  });
}

I am using Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more.

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;

This is the output i get in terminal

(node:6384) [SEQUELIZE0004] DeprecationWarning: A boolean value was passed to options.operatorsAliases. This is a no-op with v5 and should be removed. (Use node --trace-deprecation ... to show where the warning was created)
Server is running on port 8080.
Executing (default): CREATE TABLE IF NOT EXISTS users (id INTEGER NOT NULL auto_increment , username VARCHAR(255), email VARCHAR(255), password VARCHAR(255), createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM users
Executing (default): CREATE TABLE IF NOT EXISTS roles (id INTEGER , name VARCHAR(255), createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM roles
Executing (default): CREATE TABLE IF NOT EXISTS user_roles (createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL, roleId INTEGER , userId INTEGER , PRIMARY KEY (roleId, userId), FOREIGN KEY (roleId) REFERENCES roles (id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM user_roles

It hangs then

Upvotes: 0

Views: 647

Answers (1)

Gur Kamal Singh Badal
Gur Kamal Singh Badal

Reputation: 881

I believe this is not a "hang" issue. It's just the last output from sequelize, and it's been executed correctly. This is definitely a user experience problem because it "appears" frozen.

Try and access the application though and it will just work.

Upvotes: 0

Related Questions