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

264
Visualizações
Sequelize many-to-many updating junction table

I don't see how i can update an junction table with multiple id's. So i want to update a junction table so that those rows with ids that are missing leave, and new ones are created.

This is my setup

  ActorFilm.belongsTo(models.Actor, { foreignKey: 'actorId', targetKey: 'id' });
  ActorFilm.belongsTo(models.Film, { foreignKey: 'filmId', targetKey: 'id' });

  Actor.belongsToMany(models.Film, { through: models.ActorFilm, foreignKey: 'actorId' });

  Film.belongsToMany(models.Actor, { through: models.ActorFilm, foreignKey: 'filmId' });

This is how i am add new Actor:

    const actor= await Actor.create({
        userId: userId,
        title: title,
        description: description
    });

    if (filmIds && filmIds.length) {
        await actor.setFilms(filmIds );
        const actorWithFilms = Actor.findByPk(actor.id, { include: Film });
        return actorWithFilms ;
    }
    
    return actor;

And i want to update Actorlike that:

   const editedActor = await Actor.update({
        title: title,
        description: description,
    }, {
        where: {
            id: actorId
        },
        returning: true,
    });

    return editedActor;

Where i need to put new ids array so that those that are missing leave, and new ones are created in junction table. Or should I first delete the unnecessary ones myself, check those that already exist and add new ones?

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

0

Just use

await actor.setFilms(filmIds );

it deletes old link records and added new ones.

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