I'm pretty new to new sequelize but I'm trying to figure out how I can pull sequelize information from multiple tables (Place and Review tables) and render them on the same page. The Review table has a User Id and a Place Id. I've tried raw queries and different variations of the code below to no avail. What sort of syntax should I use in this case?
User.hasMany(Review);
Review.belongsTo(User);
User.hasMany(Place);
Place.belongsTo(User);
Place.hasMany(Review);
Review.belongsTo(Place);
app.get('/place/:category/:id', function(req, res){
var id = req.params.id;
Place.findAll({
where : {id : id},
include: [{
model: [Review]
}]
}).then(function(reviews){
res.render('singular', {reviews});
});
});
From your API route definition, I assume you're trying to display reviews for a place based on place ID.
So, to achieve this, you could model your table associations as
Places.hasMany(Reviews);
Users.hasMany(Reviews);
Review.belongsTo(Places);
Review.belongsTo(Users);
Now, based on this association, you could perform the query like this:
Places.findById(req.params.id, {
include: [{
model: Reviews,
required: false,
include: [{
model: Users,
required: false
}]
}]
}).then(function(place) {
// The rest of your logic here...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With