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

227
Visualizações
How to add additional attributes to a sequelize model after it has already been generated?

Is this possible or must the entire project be nuked and redone?

Example: I want to add a "breedId" column to a dogs table to reference model "Breeds"

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Dog', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      name: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      description: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Dog');
  }
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you appear to be using migrations, you will have a pretty easy time adding onto your tables.

Create another migration, and use queryInterface.addColumn(tableName: string, columnName: string, options: object) for the up, and queryInterface.removeColumn(tableName: string, columnName: string) for the down.

If you are using sequeilze.sync() to build your tables from the models, your options are worse. You either need to pass {force: true} into the .sync() method, which will drop the current tables before rebuilding them with the attributes in your models. Or, to preserve data you can omit {force: true} by manually adding the columns and foreign key constraints to the database, and add the corresponding attributes to the models.

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