How to Sync One Sequelize Model Without Impacting Others

Vinay PatelVinay Patel
2 min read

Sometimes you just want to update a single table after adding a new field — without touching the rest of your database. Here’s how to do that in Sequelize!


The Problem

You're using Sequelize ORM in your Node.js app, and you’ve added a new column (say, is_active) to your User model. If you run this:

await sequelize.sync({ alter: true });

It syncs all your models, which might be risky — especially in production.

But what if you only want to sync the User model?


Solution

Step 1: Modify your db.js (or database.js)

Make sure it only connects — no syncing here:

import { Sequelize } from "sequelize";
import dotenv from "dotenv";

dotenv.config();

const sequelize = new Sequelize(
  process.env.MYSQL_DATABASE,
  process.env.MYSQL_USER,
  process.env.MYSQL_PASSWORD,
  {
    host: process.env.MYSQL_HOST,
    dialect: "mysql",
    timezone: "+05:30",
  }
);

const connectDB = async () => {
  try {
    await sequelize.authenticate();
    console.log("MySQL connected successfully.");
  } catch (error) {
    console.error("Connection failed:", error);
    process.exit(1);
  }
};

export { sequelize, connectDB };

Step 2: Sync your User model only

In your main server.js or a custom sync.js file:

import { connectDB } from "./config/db.js";
import User from "./models/User.js";

const syncUserOnly = async () => {
  await connectDB();

  // Sync only the User model
  await User.sync({ alter: true });

  console.log("User model synced successfully.");
};

syncUserOnly();

You can also run this as a one-time script if you don’t want it in your main app.


Step 3: Define your User model as usual

Example (models/User.js):

import { DataTypes } from "sequelize";
import { sequelize } from "../config/db.js";
import Role from "./Role.js";

const User = sequelize.define("User", {
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
  },
  password: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  is_active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
  }
}, {
  timestamps: true,
});

export default User;

That's It!

You’ve now cleanly synced only the User model without touching any other tables in your database.

This is super useful when your app is already live and you want to avoid accidental changes to unrelated tables.


Bonus Tip

Want to run it as a CLI script?

  • Create a scripts/syncUser.js file

  • Add it as a script in your package.json:

"scripts": {
  "sync-user": "node scripts/syncUser.js"
}

Now just run:

npm run sync-user

Thank you for reading ✌️

0
Subscribe to my newsletter

Read articles from Vinay Patel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinay Patel
Vinay Patel

Hi, My name is Vinay Patel, and I'm an aspiring software developer.