Trouble Generating Tables Automatically in Nodejs Using Sequelize mySQL 2

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.

New contributor

Arjun Chand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật