Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda