I need to transform an SQL arguments into Sequelize
SELECT `WeekHours`.`id_weekhours` AS `idWeekhours`, `WeekHours`.`hour`, `WeekHours`.`week_day` AS `weekDay`
FROM `week_hours` AS `WeekHours`
LEFT OUTER JOIN `user_schedule` AS `UserSchedule`
ON `WeekHours`.`id_weekhours` = `UserSchedule`.`id_weekhours` AND `UserSchedule`.`date` = '2021-05-03'
WHERE `UserSchedule`.`id_weekhours` IS NULL AND `WeekHours`.`week_day` = 'Monday';
I created this in sequelize:
await WeekHours.findAll({
attributes: ['hour', 'weekDay'],
where: {
idWeekhours: {
[Op.eq]: null
},
weekDay,
},
include: [{
attributes: [],
model: UserSchedule,
where: {
date,
},
required: false,
}],
})
but i get that:
SELECT `WeekHours`.`id_weekhours` AS `idWeekhours`, `WeekHours`.`hour`, `WeekHours`.`week_day` AS `weekDay`
FROM `week_hours` AS `WeekHours`
LEFT OUTER JOIN `user_schedule` AS `UserSchedule` ON `WeekHours`.`id_weekhours` = `UserSchedule`.`id_schedule` AND `UserSchedule`.`date` = '2021-05-03'
WHERE `WeekHours`.`id_weekhours` IS NULL AND `WeekHours`.`week_day` = 'Monday';
the issues that i have is:
wrong:
`WHERE `WeekHours`.`id_weekhours
correct:
`WHERE `UserSchedule`.`id_weekhours
wrong:
`ON `WeekHours`.`id_weekhours` = `UserSchedule`.`id_schedule
correct:
`ON `WeekHours`.`id_weekhours` = `UserSchedule`.`id_weekhours
const usedHours = await WeekHours.findAll({
attributes: ['hora'],
where: {
'$UserSchedule.id_weekhours$': {
[Op.eq]: null
},
diaSemana,
},
include: [{
attributes: [],
model: UserSchedule,
on: {
id: Sequelize.where(Sequelize.col("WeekHours.id_weekhours"), "=",Sequelize.col("UserSchedule.id_weekhours"))
},
where: {
data,
},
required: false,
}],
})
Related
I have 3 tables sale,company and saleItem with the following relations:
Sale.belongsTo(Company);
Company.hasMany(Sale);
Sale.hasMany(SaleItem, { as: "items" });
SaleItem.belongsTo(Sale);
I want to apply a filter on the company's name I saw that to do that we have to use $ at the start and end but it isnt working. Any ideas where I am going wrong?
When I try to execute the below code I get the error:
SqlError: (conn=201, no: 1054, SQLState: 42S22) Unknown column 'company.name' in 'where clause'
sql: SELECT `sale`.*, `company`.`id` AS `company.id`, `company`.`name` AS `company.name`, `items`.`id` AS `items.id`, `items`.`quantity` AS `items.quantity`, `items`.`price` AS `items.price`, `items`.`description` AS `items.description`, `items`.`margin` AS `items.margin`, `items`.`gst` AS `items.gst`, `items`.`createdAt` AS `items.createdAt`, `items`.`updatedAt` AS `items.updatedAt`, `items`.`saleId` AS `items.saleId`, `items`.`itemId` AS `items.itemId` FROM (SELECT `sale`.`id`, `sale`.`date`, `sale`.`type`, `sale`.`description`, `sale`.`poNumber`, `sale`.`poDate`, `sale`.`paymentType`, `sale`.`gst`, `sale`.`discount`, `sale`.`freight`, `sale`.`status`, `sale`.`saleStatus`, `sale`.`referenceNumber`, `sale`.`ftn`, `sale`.`quotationNumber`, `sale`.`showGST`, `sale`.`invoiceDate`, `sale`.`hasWithholdingTax`, `sale`.`serialNumber`, `sale`.`currency`, `sale`.`createdAt`, `sale`.`updatedAt`, `sale`.`companyId`, `sale`.`customerId` FROM `sale` AS `sale` WHERE `company`.`name` LIKE '%%' AND `sale`.`customerId` = 1 AND `sale`.`status` = 'ACTIVE' ORDER BY `id` DESC LIMIT 0, 15) AS `sale` LEFT OUTER JOIN `company` AS `company` ON `sale`.`companyId` = `company`.`id` LEFT OUTER JOIN `saleItem` AS `items` ON `sale`.`id` = `items`.`saleId` ORDER BY `id` DESC;
It works if I dont include the SaleItem table in query
Here is the code
await Sale.findAndCountAll({
include: [
{
model: Company,
attributes: ["name"],
as: "company",
},
{ model: SaleItem, as: "items" },
],
distinct: true,
where: {
"$company.name$": { [Op.like]: `%${search}%` },
customerId:1,
status: "ACTIVE",
},
})
If you see the generated SQL, company.name WHERE clause is incorrectly added to a subquery, so you can either turn off the subquery or you can add your where option within the include.
Option 1:
await Sale.findAndCountAll({
...,
subQuery: false
})
Option 2:
await Sale.findAndCountAll({
include: [
{
model: Company,
attributes: ["name"],
as: "company",
where: {
name: { [Op.like]: `%${search}%` }
}
},
{ model: SaleItem, as: "items" },
],
distinct: true,
where: {
customerId:1,
status: "ACTIVE",
},
})
I do such node.js Sequelize query to get rows quantity of included unread_messages, so I can get amount of unread messages of specifi user. But it returns me Unknown column 'unread_messages.id' in 'field list'.
If I remove attributes: {...} error disappears
const result = await Chats.findAndCountAll({
attributes: {
include: [[Sequelize.fn('COUNT', Sequelize.col('unread_messages.id')), 'total_unread_messages']]
},
where: {
...(req.query.filters as WhereOptions),
},
include: [
{ model: Users, as: 'createdBy', required: false },
{ model: ChatTypes, as: 'type', required: false },
{
model: ChatMessages,
as: 'unread_messages',
where: {
id: {[Op.gt]: Sequelize.literal(`(
SELECT last_read_message_id
FROM chats_users
WHERE
user_id = '${req.user?.id}'
AND
chat_id = Chats.id
)`),}
},
required: false,
},
{
model: ChatMessages,
as: 'last_message',
required: false,
include: [
{ model: Users, as: 'to_user' },
{ model: Users, as: 'from_user' },
{ model: Chats, as: 'chat' },
{ model: MessageTypes, as: 'message_type' },
{
model: Users,
as: 'is_mine',
required: false,
where: { id: req.user?.id },
},
],
},
],
group:['chats.id'],
order: req.query.sort as Order,
offset,
limit,
});
i'm doing a project in nodejs (express and angular) and im finding difficulties with this sequielize. I cannot add a distinct (at the beginning of the query, i receive too much copies) and i cannot add an order by for a "include field"
I went through Sequielize documentation ( http://docs.sequelizejs.com/manual/querying.html#operators-aliases ) but without success. I found some answers in here but still no success. What am I doing wrong?
return models.Inspection.findAndCountAll({
distinct: true,
attributes: [],
include: [{model: models.InspectionGroup, attributes:['ID', 'InspectionTypeModel_ID', 'Notes', 'Date'],
required: true, include: [{
model: models.InspectionTypeModel, attributes: ['Model'], required: true
}]},
{model: models.UnitElement, attributes: [], required: true, include: [{
model: models.Span, attributes: [], required: true, include: [{
model: models.StructuralUnit, attributes: [], required: true, include: [{
model: models.Building, attributes: [], required: true, include: [{
model: models.Direction, attributes: [], required: true, include: [{
model: models.Road, attributes: [], where: {Dealer_ID: dealerList}, required: true
}]
}]
}]
}]
}]
}],
order: [[ { model: models.InspectionGroup }, 'Date', 'DESC']],
limit: limit,
offset: offset
})
.then(function (results) {
res.json(results);
})
.catch(function (err) {
commons.showerror(err, res);
});
This is the sql generated:
SELECT [Inspection].[ID], [InspectionGroup].[ID] AS [InspectionGroup.ID], [InspectionGroup].[InspectionTypeModel_ID] AS [InspectionGroup.InspectionTypeModel_ID],
[InspectionGroup].[Notes] AS [InspectionGroup.Notes], [InspectionGroup].[Date] AS [InspectionGroup.Date], [InspectionGroup.InspectionTypeModel].[ID] AS [InspectionGroup.InspectionTypeModel.ID],
[InspectionGroup.InspectionTypeModel].[Model] AS [InspectionGroup.InspectionTypeModel.Model]
FROM [Inspection] AS [Inspection]
INNER JOIN [InspectionGroup] AS [InspectionGroup] ON [Inspection].[InspectionGroup_ID] = [InspectionGroup].[ID]
INNER JOIN [InspectionTypeModel] AS [InspectionGroup.InspectionTypeModel] ON [InspectionGroup].[InspectionTypeModel_ID] = [InspectionGroup.InspectionTypeModel].[ID]
INNER JOIN [UnitElement] AS [UnitElement] ON [Inspection].[UnitElement_ID] = [UnitElement].[ID] INNER JOIN [Span] AS [UnitElement.Span] ON [UnitElement].[Span_ID] = [UnitElement.Span].[ID]
INNER JOIN [StructuralUnit] AS [UnitElement.Span.StructuralUnit] ON [UnitElement.Span].[StructuralUnit_ID] = [UnitElement.Span.StructuralUnit].[ID] INNER JOIN [Building] AS [UnitElement.Span.StructuralUnit.Building] ON [UnitElement.Span.StructuralUnit].[Building_ID] = [UnitElement.Span.StructuralUnit.Building].[ID]
INNER JOIN [Direction] AS [UnitElement.Span.StructuralUnit.Building.Direction] ON [UnitElement.Span.StructuralUnit.Building].[Direction_ID] = [UnitElement.Span.StructuralUnit.Building.Direction].[ID] INNER JOIN [Road] AS [UnitElement.Span.StructuralUnit.Building.Direction.Road] ON [UnitElement.Span.StructuralUnit.Building.Direction].[Road_ID] = [UnitElement.Span.StructuralUnit.Building.Direction.Road].[ID] AND [UnitElement.Span.StructuralUnit.Building.Direction.Road].[Dealer_ID] IN (4, 12)
ORDER BY [InspectionGroup].[Date] DESC ORDER BY [ID] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
And this is the error
Invalid usage of the option NEXT in the FETCH statement.
I can see that the sequielize generated 2 different order by, but I actually don't know why it generates the "ID order by", considering i specified that the order by should be on InspectionGroup.Date...and the "distinct:true" is totally ignored.
Edit (generated SQL with attributes: [[Sequelize.fn('distinct', Sequelize.col('ID')), 'inspectionId']])
And error: "Invalid usage of the option NEXT in the FETCH statement."
SELECT [Inspection].[ID], distinct([ID]) AS [inspectionId], [InspectionGroup].[ID] AS [InspectionGroup.ID], [InspectionGroup].[InspectionTypeModel_ID] AS [InspectionGroup.InspectionTypeModel_ID],
[InspectionGroup].[Notes] AS [InspectionGroup.Notes], [InspectionGroup].[Date] AS [InspectionGroup.Date], [InspectionGroup.InspectionTypeModel].[ID] AS [InspectionGroup.InspectionTypeModel.ID],
[InspectionGroup.InspectionTypeModel].[Model] AS [InspectionGroup.InspectionTypeModel.Model]
FROM [Inspection] AS [Inspection]
INNER JOIN [InspectionGroup] AS [InspectionGroup] ON [Inspection]. [InspectionGroup_ID] = [InspectionGroup].[ID]
INNER JOIN [InspectionTypeModel] AS [InspectionGroup.InspectionTypeModel] ON [InspectionGroup].[InspectionTypeModel_ID] = [InspectionGroup.InspectionTypeModel].[ID]
INNER JOIN [UnitElement] AS [UnitElement] ON [Inspection].[UnitElement_ID] = [UnitElement].[ID] INNER JOIN [Span] AS [UnitElement.Span] ON [UnitElement].[Span_ID] = [UnitElement.Span].[ID]
INNER JOIN [StructuralUnit] AS [UnitElement.Span.StructuralUnit] ON [UnitElement.Span].[StructuralUnit_ID] = [UnitElement.Span.StructuralUnit].[ID] INNER JOIN [Building] AS [UnitElement.Span.StructuralUnit.Building] ON [UnitElement.Span.StructuralUnit].[Building_ID] = [UnitElement.Span.StructuralUnit.Building].[ID]
INNER JOIN [Direction] AS [UnitElement.Span.StructuralUnit.Building.Direction] ON [UnitElement.Span.StructuralUnit.Building].[Direction_ID] = [UnitElement.Span.StructuralUnit.Building.Direction].[ID]
INNER JOIN [Road] AS [UnitElement.Span.StructuralUnit.Building.Direction.Road] ON [UnitElement.Span.StructuralUnit.Building.Direction].[Road_ID] = [UnitElement.Span.StructuralUnit.Building.Direction.Road].[ID] AND [UnitElement.Span.StructuralUnit.Building.Direction.Road].[Dealer_ID] IN (4, 12)
ORDER BY [Inspection].[Date] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
If i am not wrong, you should put at least one attribute on the route model to be able to apply distinct on that column. Otherwise, there is no column to apply it.
Let’s try and see if it works
distinct: true,
attributes: [´id’]
Hope it will help you
[EDIT]
With the following update it should be fine
return models.Inspection.findAndCountAll({
attributes: [[ Sequelize.fn('DISTINCT', Sequelize.col('Inspection.ID')), 'Inspection.ID']],
include: [{model: models.InspectionGroup, attributes:['ID', 'InspectionTypeModel_ID', 'Notes', 'Date'],
required: true, include: [{
model: models.InspectionTypeModel, attributes: ['Model'], required: true
}]},
{model: models.UnitElement, attributes: [], required: true, include: [{
model: models.Span, attributes: [], required: true, include: [{
model: models.StructuralUnit, attributes: [], required: true, include: [{
model: models.Building, attributes: [], required: true, include: [{
model: models.Direction, attributes: [], required: true, include: [{
model: models.Road, attributes: [], where: {Dealer_ID: dealerList}, required: true
}]
}]
}]
}]
}]
}],
order: [[ { model: models.InspectionGroup }, 'Date', 'DESC']],
limit: limit,
offset: offset
})
.then(function (results) {
res.json(results);
})
.catch(function (err) {
commons.showerror(err, res);
});
I want to do this:
select sum("quantity") as "sum"
from "orderArticles"
inner join "orders"
on "orderArticles"."orderId"="orders"."id"
and "orderArticles"."discountTagId" = 2
and "orders"."paid" is not null;
which results in on my data base:
sum
-----
151
(1 row)
How can I do it?
My Sequelize solution:
The model definitions:
const order = Conn.define('orders', {
id: {
type: Sequelize.BIGINT,
autoIncrement: true,
primaryKey: true
},
// ...
paid: {
type: Sequelize.DATE,
defaultValue: null
},
// ...
},
// ...
})
const orderArticle = Conn.define('orderArticles',
{
id: {
type: Sequelize.BIGINT,
autoIncrement: true,
primaryKey: true
},
// ...
quantity: {
type: Sequelize.INTEGER,
defaultValue: 1
}
},
{
scopes: {
paidOrders: {
include: [
{ model: order, where: { paid: {$ne: null}} }
]
}
},
// ...
})
Associations:
orderArticle.belongsTo(order)
order.hasMany(orderArticle, {onDelete: 'cascade', hooks: true})
I came up with this after hours of research:
db.models.orderArticles
.scope('paidOrders') // select only orders with paid: {$ne: null}
.sum('quantity', { // sum up all resulting quantities
attributes: ['quantity'], // select only the orderArticles.quantity col
where: {discountTagId: 2}, // where orderArticles.discountTagId = 2
group: ['"order"."id"', '"orderArticles"."quantity"'] // don't know why, but Sequelize told me to
})
.then(sum => sum) // return the sum
leads to this sql:
SELECT "orderArticles"."quantity", sum("quantity") AS "sum",
"order"."id" AS "order.id", "order"."taxRate" AS "order.taxRate",
"order"."shippingCosts" AS "order.shippingCosts", "order"."discount"
AS "order.discount", "order"."paid" AS "order.paid",
"order"."dispatched" AS "order.dispatched", "order"."payday" AS
"order.payday", "order"."billNr" AS "order.billNr",
"order"."createdAt" AS "order.createdAt", "order"."updatedAt" AS
"order.updatedAt", "order"."orderCustomerId" AS
"order.orderCustomerId", "order"."billCustomerId" AS
"order.billCustomerId" FROM "orderArticles" AS "orderArticles" INNER
JOIN "orders" AS "order" ON "orderArticles"."orderId" = "order"."id"
AND "order"."paid" IS NOT NULL WHERE "orderArticles"."discountTagId" =
'4' GROUP BY "order"."id", "orderArticles"."quantity";
which has this result on the same data base: 0 rows
If you know what I got wrong please let me know!
Thank you :)
Found the solution:
in the scopes definition on the orderArticle model:
scopes: {
paidOrders: {
include: [{
model: order,
where: { paid: {$ne: null}},
attributes: [] // don't select additional colums!
}]
}
},
//...
and the algorithm:
db.models.orderArticles
.scope('paidOrders')
.sum('quantity', {
attributes: [], // don't select any further cols
where: {discountTagId: 2}
})
Note: In my case it was sufficient to return the promise. I use GraphQL which resolves the result and sends it to the client.
It is impossible to filter data using a linked table. There are two tables Instructor and Club. They related how belongsToMany. I need to get all Instructors which club_id = value.
Instructor model:
sequelize.define('Instructor', {
instance_id: DataTypes.INTEGER,
name: DataTypes.STRING(255)
}, {
tableName: 'instructors',
timestamps: false,
classMethods: {
associate: function (models) {
Instructor.belongsToMany(models.Club, {
through: 'InstructorClub'
});
}
}
});
Club model:
sequelize.define('Club', {
instance_id: DataTypes.INTEGER,
name: DataTypes.STRING
}, {
tableName: 'clubs',
timestamps: false,
classMethods: {
associate: function (models) {
Club.belongsToMany(models.Instructor, {
through: 'InstructorClub'
});
}
}
});
Related table:
sequelize.define('InstructorClub', {
InstructorId: {
type: DataTypes.INTEGER,
field: 'instructor_id'
},
ClubId: {
type: DataTypes.INTEGER,
field: 'club_id'
}
}, {
tableName: 'instructors_clubs'
timestamps: false
});
I am trying to get the data as follows::
models
.Instructor
.findAll({
include: [
{
model: models.Club,
as: 'Clubs',
through: {
attributes: []
}
}
],
# I need to filter by club.id
where: {
'Clubs.id': 10
}
})
Current query generated SQL:
SELECT `Instructor`.`id`,
`Instructor`.`instance_id`,
`Instructor`.`name`,
`Clubs`.`id` AS `Clubs.id`,
`Clubs`.`name` AS `Clubs.name`,
`Clubs.InstructorClub`.`club_id` AS `Clubs.InstructorClub.ClubId`,
`Clubs.InstructorClub`.`instructor_id` AS `Clubs.InstructorClub.InstructorId`
FROM `instructors` AS `Instructor`
LEFT OUTER JOIN (`instructors_clubs` AS `Clubs.InstructorClub` INNER JOIN `clubs` AS `Clubs` ON `Clubs`.`id` = `Clubs.InstructorClub`.`club_id`)
ON `Instructor`.`id` = `Clubs.InstructorClub`.`instructor_id`
WHERE `Instructor`.`Clubs.id` = 10;
Well, I need some kind of this:
SELECT `Instructor`.`id`,
`Instructor`.`instance_id`,
`Instructor`.`name`,
`Clubs`.`id` AS `Clubs.id`,
`Clubs`.`name` AS `Clubs.name`,
`Clubs.InstructorClub`.`club_id` AS `Clubs.InstructorClub.ClubId`,
`Clubs.InstructorClub`.`instructor_id` AS `Clubs.InstructorClub.InstructorId`
FROM `instructors` AS `Instructor`
LEFT OUTER JOIN (`instructors_clubs` AS `Clubs.InstructorClub` INNER JOIN `clubs` AS `Clubs` ON `Clubs`.`id` = `Clubs.InstructorClub`.`club_id`)
ON `Instructor`.`id` = `Clubs.InstructorClub`.`instructor_id`
# It should be like this:
WHERE `Clubs`.`id` = 10;
Move your 'where' up into the include (with model, as, and through).
include: [ {
model: models.Club,
as: 'Clubs',
through: { attributes: [] },
where: { 'Clubs.id': 10 }
} ]