You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Query with nested include and custom join condition #1869
summary.StudentId = profile.id is already supported obviously but yes summary.version = profile.version might be an issue, but should not be too hard to implement actually.
Reacted by NoBonus, Dmitry Sorokin, Dimitri Dessus, Brian Hague, Antonio Moura, CR1SPR, Anwaar Ul Haq, Evandro Terra, priyankajss-sdei and Mihai Radulescu
@janmeier Thanks. 'through' and 'required: true' helped me.
My Example:
I need 'Sites' only with 'Craft' association, where 'Craft' entry exists and (Site.planExpiredDate > current date).
and instead of all kinds of messy nested JS code to make mess SQL code... 1 JOIN, 2 WHEREs no subqueries easily stacked with AND by default like other native queries
SELECT *
FROM table JOIN anotherTable ON table.fkid = anotherTable.pkid
WHERE table.field = "value" AND anotherTable.anotherField = "anotherValue" ;
...this lib constantly makes me do all kinds of extra loops and object manipulation, strange native static methods AND/OR raw queries.
@cesar2064 I know i bitch a lot but I really do appreciate this lib. I was actually in the midst of doing exactly what you suggested before finding Sequelize and moving to it in a time crunch. but right now reporting is all done in raw queries and filtering is becoming mind-numbing.
I am in the middle of building function to format queries like the above into something to filter related tables with. I'll come back with something useful in the next day or so even it uses raws.
i ended up building a raw filter query of just ids... dynamically adding joins and where sections depending on the object passed in into an array of ids then passed it into the original query like this:
list => {
let q = [];
for(i in list) {
q.push(list[i].id);
}
return Model.findAll({where: {id: q}, include: [<fun list of relations>]})
I have four tables join by belongsTo relationship. location belongs to city, city belongs to state and state belongs to country. when i search for a location by id and wish to get city, state and country in the response then I include these model in nested include format like the following.
What I did here is to run a subquery that get all the workflow id that meets the necessary conditions in the Action table(It's a one to many table).If the condition is met in one of the many data, I select the Id and then use the main query to generate the necessary data base on those workflowid selected from the Actions table.
Activity
mickhansen commentedon Jun 6, 2014
Querying on the through model/join table is not possible currently unfortuneatly.
lucaswxp commentedon Sep 25, 2014
I tried querying associations with conditions and it worked:
Shouldn't this be closed?
mickhansen commentedon Sep 25, 2014
@lucaswxp the issue is about filtering on join table models - You example is just filtering on the target model :)
lvarayut commentedon Jan 23, 2015
+1 I'm having the same issue. It's would be nice to have this feature.
mickhansen commentedon Jan 23, 2015
@lvarayut what does your code look like? It's possible to ammend the join conditions with
include.where, not overwrite, do you need to overwrite?lvarayut commentedon Jan 23, 2015
@mickhansen I want to join two tables using
andcondition as following:mickhansen commentedon Jan 23, 2015
@lvarayut i see
summary.StudentId = profile.idis already supported obviously but yessummary.version = profile.versionmight be an issue, but should not be too hard to implement actually.Perhaps an API like:
mickhansen commentedon Jan 23, 2015
Perhaps you could already do it like:
lvarayut commentedon Jan 23, 2015
Thanks @mickhansen for your quick response. I'ill try it soon. :)
eclipse1985 commentedon Jul 26, 2015
Is querying on the through model/join table now possible with version 3.*?
janmeier commentedon Jul 26, 2015
@eclipse1985 Yes
include: [ { model .., through: { where: .. } }]eclipse1985 commentedon Jul 26, 2015
Oh thanks, from which version exactly?
7 remaining items
cesar2064 commentedon Aug 2, 2017
I know is not the best solution, but I have solved a similar issue with this implementation:
This is with sequelize 4.4.2, I don't know if it is working in previous versions.
cybercarnage commentedon Sep 15, 2017
@janmeier Thanks. 'through' and 'required: true' helped me.
My Example:
I need 'Sites' only with 'Craft' association, where 'Craft' entry exists and (Site.planExpiredDate > current date).
associate:
SiteCategoryName.belongsToMany(models.Site, {as: 'Sites', through: 'SiteCategory', foreignKey: 'categoryId'});Site.hasOne(models.CraftSite, { as: 'Craft', foreignKey: 'siteId' });This worked for me
models.SiteCategoryName.findAll({ through: { where: { planExpiredDate: { $gt: new Date() } } }, include: [ { model: models.Site, as: 'Sites', include: [ { model: models.CraftSite, as: 'Craft', required: true } ] } ], limit: process.env.ADMIN_PER_PAGE // 10 })jeremybradbury commentedon Jan 11, 2018
why doesn't this work?
and instead of all kinds of messy nested JS code to make mess SQL code... 1 JOIN, 2 WHEREs no subqueries easily stacked with
ANDby default like other native queries...this lib constantly makes me do all kinds of extra loops and object manipulation, strange native static methods AND/OR raw queries.
cesar2064 commentedon Jan 11, 2018
nice suggestion, @jeremybradbury, if you dont like it you are free to use a different one or create your own lib..
jeremybradbury commentedon Jan 12, 2018
@cesar2064 I know i bitch a lot but I really do appreciate this lib. I was actually in the midst of doing exactly what you suggested before finding Sequelize and moving to it in a time crunch. but right now reporting is all done in raw queries and filtering is becoming mind-numbing.
I am in the middle of building function to format queries like the above into something to filter related tables with. I'll come back with something useful in the next day or so even it uses raws.
jeremybradbury commentedon Jan 12, 2018
i ended up building a raw filter query of just ids... dynamically adding joins and where sections depending on the object passed in into an array of ids then passed it into the original query like this:
where
listis the raw query resultnow my brain hurts
bharatkapse commentedon Aug 8, 2018
I have four tables join by belongsTo relationship. location belongs to city, city belongs to state and state belongs to country. when i search for a location by id and wish to get city, state and country in the response then I include these model in nested include format like the following.
router.get('/getLocation/:id',function(req,res){
location.findOne({
where:{id:req.params.id},
include:[{
model:city,
attributes:['name'],
include:[{
model:state,
attributes:['name'],
include:[{
model:country,
attributes:['name']
}]
}]
}]
}).then(function(Location){
res.json(Location);
});
});
And the response I am getting is like the following
{
"id": "1",
"displayname": "VimanNagar",
"name": "VimanNagar",
"city_id": "3",
"city": {
"name": "Pune",
"state": {
"name": "Maharashtra",
"country": {
"name": "India"
}
}
}
}
How can I get the response as following
{
"id": "1",
"displayname": "VimanNagar",
"name": "VimanNagar",
"city":"Pune",
"state":"Maharashtra",
"country":"India"
}
please help me out here
BolajiOlajide commentedon Dec 28, 2018
@jacinto123 Were you able to get a fix?
BolajiOlajide commentedon Dec 28, 2018
I was able to get around this by using this query instead
niyasshefeek commentedon May 6, 2019
what to do with this case ?
3 table Join and One table has value Json Type and joining other table using the JSON value.
` SELECT a.idListing,a.listingtitle,a.listingImage,c.name
FROM listing a LEFT JOIN listingcategory b ON a.listingCategory = b.idListingCategory
LEFT JOIN offercateogry c ON JSON_CONTAINS(a.offercategory, CAST(c.idofferCateogry as JSON), '$'); `
idmega2000 commentedon Apr 11, 2020
I had the same issue and I was able to fix it with.
What I did here is to run a subquery that get all the workflow id that meets the necessary conditions in the Action table(It's a one to many table).If the condition is met in one of the many data, I select the Id and then use the main query to generate the necessary data base on those workflowid selected from the Actions table.