Is anyone successful using nested include along with Limit and Offset in Sequelize. I'm trying to implement server side paging using the Sequelize, can anyone show me any reference. I'm using Sql Server database. I see that the query is being converted as sub query along with joins when I try to do this. Has anyone got
{where: query.activity,
attributes: [...activityAttributes, 'LastModifiedUserID', 'LastModifiedDateUTC', 'SPIStatus'],
include: [
{
model: Issue,
where: query.issue,
attributes: issueAttributes,
include: [{
model: Product,
where: query.product,
attributes: productAttributes
},
{
model: IssueExtendedAttribute,
where: {$and: query.issueExtendedAttributes},
required: !!query.issueExtendedAttributes
}]
}],
offset: 10,
limit: 10}
You need to add
subQuery: false;
ex:
{
subQuery: false,
where: queryObj,
include: [{
model: db.A,
where: AQueryObj,
include:[{
model: db.B,
where: BQueryObj
},{
model: db.C,
where: CQueryObj
}]
}],
offset: offset,
limit: limit
}
Check this link to get more information
Related
I trying to fetch the assessment details for a class in which assessment_test model has association with student_assessment_test model(hasMany). I want the fetched result ordered according to the marks obtained by student. I am trying the below method but it doesn't seem to be working. Please let me know the correct method for such queries.
const assessmentDetails = await Entity.assessmentTest.findOne({
where: { id: assessmentId, status: 1 },
attributes: [
'id',
'sectionId',
'assessmentDate',
'assessmentTitle',
'totalPoints',
],
include: [
{
association: 'assessmentTestDetails',
where: { status: 1 },
attributes: ['marksObtained', 'percentage', 'testStatus'],
required: true,
order: [['marksObtained', 'ASC']],
include: [
{
association: 'student',
where: { status: 1 },
attributes: ['studentName'],
required: true,
},
],
},
],
I have 4 tables Properties, Units, Leases, LeaseRates.
Units
Leases
LeaseRates
propertyId
unitId
leaseId
they are all connected with the id of the last one.
I have these relationships setup like this
Properties.hasMany(Units);
Units.belongsTo(Properties);
Units.hasMany(Leases);
Leases.belongsTo(Units);
Leases.hasMany(LeaseRates);
LeaseRates.belongsTo(Leases);
Is there an easy way in Sequelize for me to query LeaseRates via propertyId?
<pre>
await properties.findOne({
where: { id },
include: [{
model: model.Unit, as: 'unit',
attributes: ['id'],
include: [{
model: model.Leases, as: 'leases',
attribute: ['id'],
include: [{ model: model.LeaseRates, as: 'leaserates' }]
}]
}]
})
</pre>
I have an array=[1,2,3]
Only 1 & 2 are in the database under the column ID.
When I perform a query using WHERE IN it gives me result of 1 & 2.
What I want to do is if all data matches then show me the result otherwise null
I am using Sequelize. How can this be done?
Query:
Models.Question.findOne({
where: {
id:1
},
include: [{
model: Models.QuestionOptions,
as: 'options',
required: true,
where: {
id:[1,2,3]
}
}]
});
expect result Null because only 1 & 2 are in DB
require:true means INNER JOIN. In other words it will only return lines with existing associations(If the condition is also true of course). You should remove it if you want to get all lines(Left Join) even if there is no corresponding association
You can achieve this using group and having clause.
Models.Question.findOne({
attributes: ["id"],
where: {
id:1
},
group: ['id'],
having: sequelize.where(sequelize.fn('count', sequelize.col('options.id')), {
[sequelize.Op.gte]: 3,
}),
include: [{
model: Models.QuestionOptions,
as: 'options',
attributes: [],
required: true,
where: {
id:[1,2,3]
}
}]
});
i have a sequelize query like this. however, when I add limit and offset there is an error like this.
what should i do?
const courses = await DB_COURSES.findAll({
attributes: ["id"],
include:[
{
model: DB_COURSETRANSACTIONS,
attributes:["coursePurchasedId"],
include:[
{
model:DB_COURSES,
as: 'coursePurchased',
include:[
{
model: DB_COMPANIES,
as: 'buyerByCompany',
attributes:["id"]
}
],
required: true
}
],
required: true
},
{
model: DB_COMPANIES,
as: 'authorByCompany',
attributes: ["id"]
}
],
limit: 10,
offset: 0
})
error message
ERD table
I want like this:
like this
I'm trying to perform the following query using Sequelize:
db.Post.findAll({
include: [
{
model: db.User,
as: 'Boosters',
where: {id: {[Op.in]: a_set_of_ids }}
},
{
model: db.Assessment,
as: 'PostAssessments',
where: {UserId: {[Op.in]: another_set_of_ids}}
}
],
attributes: [[db.sequelize.fn('AVG', db.sequelize.col('Assessments.rating')), 'average']],
where: {
average: 1
},
group: ['id'],
limit: 20
})
But I run to this error: "ER_BAD_FIELD_ERROR". Unknown column 'Assessments.rating' in 'field list', although I do have table "Assessments" in the database and "rating" is a column in that table.
My Post model looks like this:
const Post = sequelize.define('Post', {
title: DataTypes.TEXT('long'),
description: DataTypes.TEXT('long'),
body: DataTypes.TEXT('long')
}, {
timestamps: false
});
Post.associate = function (models) {
models.Post.belongsToMany(models.User, {as: 'Boosters', through: 'UserPostBoosts' });
models.Post.hasMany(models.Assessment, {as: 'PostAssessments'});
};
What am I doing wrong?
It seems like this problem surfaces when we have a limit in a find query where associated models are included (the above error doesn't show up when we drop the limit from the query). To solve that, we can pass an option subQuery: false to the find. (https://github.com/sequelize/sequelize/issues/4146)
This is the correct query in case anyone comes across the same problem:
db.Post.findAll({
subQuery: false,
include: [
{
model: db.User,
as: 'Boosters',
where: {id: {[Op.in]: a_set_of_ids }}
}
,{
model: db.Assessment,
as: 'PostAssessments',
where: {UserId: {[Op.in]: another_set_of_ids}}
}
],
having: db.sequelize.where(db.sequelize.fn('AVG', db.sequelize.col('PostAssessments.rating')), {
[Op.eq]: 1,
}),
limit: 20,
offset: 2,
group: ['Post.id', 'Boosters.id', 'PostAssessments.id']
})
Error is with this one :
models.sequelize.col('Assessments.rating'))
Change it to
models.sequelize.col('PostAssessments.rating')) // or post_assessments.rating
Reason : You are using the alias for include as: 'PostAssessments',.