Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
How to force Sequelize Model.init to work?

I'm trying to initialize database table according model, but table can't be created, and there is no any errors. Furthermore, after this action, sequelize.models object has AccountsModel.

import { DataTypes, Model, Sequelize } from 'sequelize'
import { DatabaseConfig } from '../../database/config'

export class AccountsModel extends Model { }

export function initializeAccountsModel (sequelize: Sequelize): void {
    AccountsModel.init({
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false,
        },
    }, {
        sequelize,
        tableName: DatabaseConfig.TABLE_NAMES.Accounts,
    })   
}

Model.sync() method is working, but I'm gonna follow OOP.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The .sync() method is the action that actually creates the tables when you start your application. It goes into your models directory and builds the tables based on the attributes you've specified in the individual models. If you don't run .sync(), this action will not occur.

If you wish to not use .sync(), the alternative is to either manually create the tables (Not great if you ever need to re-deploy for some reason), or utilize migrations.

Another important note regarding .sync() is that it will use the SQL query CREATE TABLE IF NOT EXISTS when building from the models, so if you have added attributes to the model in between syncs, these new columns will not be added to the table when you re-sync. You can bypass this by passing {force: true} into .sync(), but that will first DROP the table and then CREATE it again, causing data loss.

Migrations are strongly recommended for production applications.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!