Skip to content

Instantly share code, notes, and snippets.

@santi1976
Created July 28, 2022 15:48
Show Gist options
  • Save santi1976/35413d99e2192a9c7ec35672f0976415 to your computer and use it in GitHub Desktop.
Save santi1976/35413d99e2192a9c7ec35672f0976415 to your computer and use it in GitHub Desktop.
-- // PELICULAS DE ACTOR // -----------------------------
// tu código aquí
const PeliculaActor = sequelize.define('pelicula_actor',{
pelicula_id: {
type: Sequelize.INTEGER,
references: {
model: 'Pelicula',
key: 'id'
}
},
actor_id: {
type: Sequelize.INTEGER,
references: {
model: 'Actor',
key: 'id'
}
}
});
module.exports = PeliculaActor;
------- RELACIONES PIVOT ------------------------------
// Tu códigO aquí
PeliculaActor.belongsTo(Pelicula, {
foreignKey: "pelicula_id"
})
PeliculaActor.belongsTo(Actor, {
foreignKey: "actor_id"
})
module.exports = PeliculaActor;
-- // PELICULAS, ACTOR TOMA 3 // -----------------------------------
// tu código aquí
Pelicula.belongsToMany(Actor, {
as:"actores",
through:"PeliculaActor",
foreignKey:"pelicula_id"
})
module.exports = Pelicula;
////////////// BUSCANDO ACTORES ---------------------------------
// tu código aquí
Pelicula.findByPk(1, {
include:["actores"]
}).then(function(pelicula){
console.log(pelicula.actores)
})
/////////////////// AGREGANDO ACTORES ---------------------------------
// tu código aquí
pelicula.setActores([3, 5, 8])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment