I'm trying to relate, the table of characters and movie. When I do a get to show the movies and related characters it shows me the error . SequelizeEagerLoadingError: character is not associated to movie! The movie primary key is in the title column.
Pelicula.associate = models=>{
Pelicula.belongsToMany(models.Personaje)
}
The character's primary key is in name
Personaje.associate = models => {
Personaje.belongsToMany(models.Pelicula)
}
GET-API
router.get('/movies',async(req,res)=>{
const peliculas = await Pelicula.findAll({
attributes:['imagen','titulo','fechaCreacion','personaje'] ,
include:[{model:Personaje}]
}).then(peliculas=>{
return res.status(200).json({
peliculas
});
}).catch(error=>{
console.log("ERROR! "+error);
})
})
From the official documentation :
Applied to your code:
Y
Where
Pelicula_Personajes
is the name of the intermediate table that establishes the relationships between the two entities.If the relationship is a movie with many characters , your code would be:
To return all the movies with their respective characters: