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

588
Visualizações
Sequelize 6, delete cascade with M:N association

I'm trying to define some specific M:N association using sequelize v6.20.1 and i'm facing and issue...

I have 2 models, an Account model and a Group model. My rules about these models are the following:

  • An account can exists without any group
  • An account can have multiple groups
  • A group can exists with a least one account associated, so that mean a group cannot exists without an account associated
  • A group can be associated with multiple accounts

Here is the code definition of all models and association :

const Sequelize, { Model, DataTypes } = require('sequelize');

const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: `./database.sqlite`,
});

/* ----- Account model ----- */

class Account extends Model {
  // Some account's methods definitions...
}
Account.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  name: DataTypes.STRING,
  username: DataTypes.STRING,
  email: DataTypes.STRING,
  password: DataTypes.STRING,
}, { sequelize });

/* ----- Group model ----- */

class Group extends Model {
  // Some group's methods definitions...
}
Group.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  name: DataTypes.STRING,
}, { sequelize });

/* ----- AccountGroup model ----- */

class AccountGroup extends Model {
}
AccountGroup.init({
  AccountId: {
    type: DataTypes.INTEGER,
    references: {
      model: Account,
      key: 'id',
    },
    onDelete: 'RESTRICT',
  },
  GroupId: {
    type: DataTypes.INTEGER,
    references: {
      model: Group,
      key: 'id',
    },
    allowNull: false,
    onDelete: 'CASCADE',
  },
}, {
  sequelize,
  timestamps: false,
});

/* ----- Association definition ----- */

Account.belongsToMany(Group, { through: AccountGroup });
Group.belongsToMany(Account, { through: AccountGroup });

sequelize.authenticate();
sequelize.sync({ force: true };

(async () => {
  const group = new Group({ name: 'Group' });
  await group.save();

  const account = new Account({ name: 'Doe', username: 'John', email: 'john.doe@example.com', password: 'secret' });
  account.addGroup(group);
  await account.save();

  // some processing code...

  await account.destroy();
})();

After account.destroy() finished, the AccountGroup row is successfully deleted but not the Group. And i want to delete unreferenced groups.

What am i missing ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is the way that the cascading deletes works. In your example, when the Account is deleted, rows in the AccountGroup table may now have an invalid value in their AccountId foreign key column. You are telling the database that when this situation occurs, delete the AccountGroup entirely. Similarly, if you delete a Group, this will cascade down and delete any AccountGroup with that Group as its GroupId.

No such issue arises for the Account or Group tables when an AccountGroup is deleted. They do not contain any foreign keys that have been made invalid.

To find the functionality that you are searching for, deleting any groups that no longer belong to an AccountGroup, you will likely want to put a separate query in your code, or you may be able to use a Hook to be executed after an Account is deleted.

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