I’m encountering an issue where I’m unable to automatically create an “employee” table using Sequelize in my Node.js application. Strangely, while my “user” table gets created automatically, the “employee” table does not, resulting in an error message indicating that the table is not present when I call any API.
I’ve attached screenshots of my file for reference, showcasing the relevant code and configurations. Despite following similar procedures for both tables, I’m puzzled by this discrepancy and seeking assistance to resolve the issue
here is my Employee Table
const { DataTypes } = require('sequelize');
const sequelize = require('../database.config'); // Adjust path as necessary
const Employee = sequelize.define(
'Employee',
{
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
phone: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
city: {
type: DataTypes.STRING
},
state: {
type: DataTypes.STRING
},
country: {
type: DataTypes.STRING,
allowNull: false
},
gender: {
type: DataTypes.STRING,
validate: {
isIn: [['Male', 'Female', 'Other']]
}
},
education: {
type: DataTypes.STRING
},
hobbies: {
type: DataTypes.ARRAY(DataTypes.STRING)
}
},
{
tableName: 'Employee', // Set the table name
timestamps: false // Disable timestamps if not needed
}
);
module.exports = Employee;
it uses database.config
const { Sequelize } = require("sequelize");
const dotenv = require("dotenv").config({ path: '.env.dev' });
if (dotenv.error) {
throw new Error("Error loading environment variables from .env.dev file");
}
console.log("DB Password: " + process.env.DB_PASSWORD);
console.log("DB Name: " + process.env.DB_NAME);
const sequelize = new Sequelize(
process.env.DB_NAME,
"root",
process.env.DB_PASSWORD,
{
host: "localhost",
dialect: "mysql",
logging: false
},
);
(async () => {
try {
await sequelize.sync({ alter: true });
console.log("Database synchronized successfully.");
} catch (error) {
console.error("Error synchronizing database:", error);
}
})();
module.exports = sequelize;
and here is my main server.js
const express = require("express");
const bodyParser = require("body-parser");
const sequelize = require("./db/database.config");
const cors = require("cors");
const app = express();
const path = require("path");
const routePaths = require("./config/route_paths.config");
const morgan = require("morgan");
const dotenv = require("dotenv").config({path:'./.env.dev'});
const port = process.env.PORT;
const cookieParser = require('cookie-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan("dev"));
app.use(cookieParser());
const corsOptions = {
origin: 'http://localhost:5173', // Your React app's origin
credentials: true, // Allow credentials (cookies)
};
app.use(cors(corsOptions));
// Serve static folders
app.get("/", (req, res) => {
res.send("Server Running");
});
// Dynamically include routes
(async () => {
const resolvedPaths = await routePaths;
resolvedPaths.forEach((route) => {
app.use(require(route));
});
})();
app.listen(port, async function () {
console.log("Server is running on port " + port);
sequelize
.authenticate()
.then(() => {
console.log("Connection has been established successfully.");
})
.catch((err) => {
console.error("Unable to connect to the database:", err);
});
});
Any insights or suggestions on how to troubleshoot and resolve this problem would be greatly appreciated. Thank you!
I expected the “employee” table to be automatically created and synchronized with the database schema when I called any API that interacts with it. However, I received an error message stating that the “employee” table was not present, indicating that it failed to be created automatically.
Arjun Chand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.