How to configure hasMany relationship with many-to-many table - node.js

I have the following setup:
Organization (1)----(*) OrganizationArticleItemMap (*)----(1) ArticleItem
(1)
|
|
|
(*)
ArticleItemPriceRule
An article item can thus belong to many organizations and an organization can have many article items. For every article item in an organization there will be multiple price rules.
The many to many relationship has been configured as such:
this.models.ArticleItem.belongsToMany(this.models.Organization, {
through: this.models.OrganizationArticleItemMap,
foreignKey: 'ArticleItemId'
});
this.models.Organization.belongsToMany(this.models.ArticleItem, {
through: this.models.OrganizationArticleItemMap,
foreignKey: 'OrganizationId'
});
I do not really know how I should configure ArticleItemPriceRule so that I can fetch the Price rules for all articles for a given organization.
I have tried the following :
this.models.ArticleItem.hasMany(this.models.OrganizationArticleItemMap, {
foreignKey: 'ArticleItemId',
as: 'OrganizationMaps'
});
this.models.OrganizationArticleItemMap.hasMany(this.models.ArticleItemPriceRule, {
as: 'Prices',
foreignKey: 'Organizations_articleitem_map_Id'
});
and then the following query:
DataAccess.dataContext.models.ArticleItem.findAll({
include: [
{
model: DataAccess.dataContext.models.Organization,
where: {OrganizationId: '1'},
attributes: [],
through: {
attributes: []
}
},
{
model: DataAccess.dataContext.models.OrganizationArticleItemMap,
as: 'OrganizationMaps',
required: true,
include: [{
model: DataAccess.dataContext.models.ArticleItemPriceRule,
as: 'Prices',
required: false
}]
}
],
where: {ArticleItemId: '1'}
})
The problem with this is that the sql query that was generated included an inner join with OrganizationArticleItemMap twice.
SELECT `ArticleItem`.`ArticleItemId`,
`ArticleItem`.`ArticleCategoryId`,
`ArticleItem`.`VisibleOnOnlineBooking`,
`OrganizationMaps`.`Organization_articleitem_map_Id` AS `OrganizationMaps.OrganizationArticleItemMapId`,
`OrganizationMaps`.`OrganizationId` AS `OrganizationMaps.OrganizationId`,
`OrganizationMaps`.`ArticleItemId` AS `OrganizationMaps.ArticleItemId`,
`OrganizationMaps.Prices`.`ArticleItemPriceRuleId` AS `OrganizationMaps.Prices.ArticleItemPriceRuleId`,
`OrganizationMaps.Prices`.`Organizations_articleitem_map_Id` AS `OrganizationMaps.Prices.Organizations_articleitem_map_Id`,
`OrganizationMaps.Prices`.`CurrencyId` AS `OrganizationMaps.Prices.CurrencyId`,
`OrganizationMaps.Prices`.`Price` AS `OrganizationMaps.Prices.Price`,
`OrganizationMaps.Prices`.`ValidFrom` AS `OrganizationMaps.Prices.ValidFrom`
FROM `articleitem` AS `ArticleItem`
INNER JOIN (`organization_articleitem_map` AS `Organizations.OrganizationArticleItemMap`
INNER JOIN `organizations` AS `Organizations` ON `Organizations`.`OrganizationId` = `Organizations.OrganizationArticleItemMap`.`OrganizationId`) ON `ArticleItem`.`ArticleItemId` = `Organizations.OrganizationArticleItemMap`.`ArticleItemId`
AND `Organizations`.`OrganizationId` = '1'
AND `Organizations`.`IsDeleted` = 0
INNER JOIN `organization_articleitem_map` AS `OrganizationMaps` ON `ArticleItem`.`ArticleItemId` = `OrganizationMaps`.`ArticleItemId`
LEFT OUTER JOIN `articleitempricerule` AS `OrganizationMaps.Prices` ON `OrganizationMaps`.`Organization_articleitem_map_Id` = `OrganizationMaps.Prices`.`Organizations_articleitem_map_Id`
AND `OrganizationMaps.Prices`.`IsDeleted` = 0
WHERE `ArticleItem`.`ArticleItemId` = '1'
AND `ArticleItem`.`IsDeleted` = 0;

Well, this is the response I got from the sequelize team on github:
Sequelize doesn't know that those two relations uses the same tables.
You look to be doing it correctly, we just aren't smart enough to
infer that we could use the join table just a single time.
https://github.com/sequelize/sequelize/issues/5376

Related

Sequelize column reference is ambiguous

I'm trying to accomplish the task of joining all associated models, and then retrieving the count of all the models
I would want the data to be returned as
product {
main_dish: {},
side_dish: {},
drink: {},
purchase_product_count: x,
purchased_products: []
}
I have constructed this in sequelize to try to do this.
await Product.findAll({
include: [{
model: MainCourse,
required: false,
}, {
model: SideDish,
required: false,
}, {
model: Drink,
required: false,
}, {
model: ProductPurchase,
required: false,
where: {
createdAt: {[Op.between]: [two_days_before, current_date]}
},
}],
attributes: {
include: ['*',
[Sequelize.literal(
"(SELECT COUNT(*) from product_purchases as p where p.purchase_product_id = products.product_id)"), "product_purchase_count"],
]
},
offset: 1 * 5,
limit: 5
})
.then((results) => {
console.log(results)
res.status(200).json(results)
})
.catch((err) => {
console.log(err)
res.status(400).json(err)
})
Attempting to run this eager load gives this error
column reference "product_id" is ambiguous
This is the sql generated by sequelize
SELECT "products".*, "main_course"."main_course_id" AS "main_course.main_course_id", "main_course"."type" AS "main_course.type", "main_course"."createdAt" AS "main_course.createdAt", "main_course"."updatedAt" AS "main_course.updatedAt", "main_course"."deletedAt" AS "main_course.deletedAt", "main_course"."main_product_id" AS "main_course.main_product_id", "side_dish"."side_dish_id" AS "side_dish.side_dish_id", "side_dish"."type" AS "side_dish.type", "side_dish"."createdAt" AS "side_dish.createdAt", "side_dish"."updatedAt" AS "side_dish.updatedAt", "side_dish"."deletedAt" AS "side_dish.deletedAt", "side_dish"."side_product_id" AS "side_dish.side_product_id", "drink"."drink_id" AS "drink.drink_id", "drink"."type" AS "drink.type", "drink"."createdAt" AS "drink.createdAt", "drink"."updatedAt" AS "drink.updatedAt", "drink"."deletedAt" AS "drink.deletedAt", "drink"."drink_product_id" AS "drink.drink_product_id", "product_purchases"."product_purchase_id" AS "product_purchases.product_purchase_id", "product_purchases"."price" AS "product_purchases.price", "product_purchases"."quantity" AS "product_purchases.quantity", "product_purchases"."createdAt" AS "product_purchases.createdAt", "product_purchases"."updatedAt" AS "product_purchases.updatedAt", "product_purchases"."deletedAt" AS "product_purchases.deletedAt", "product_purchases"."purchase_product_id" AS "product_purchases.purchase_product_id", "product_purchases"."restaurant_order_id" AS "product_purchases.restaurant_order_id" FROM (SELECT "products"."product_id", "products"."title", "products"."price", "products"."createdAt", "products"."updatedAt", "products"."deletedAt", "products"."restaurant_id", "products".*, (SELECT COUNT(*) from product_purchases as p where p.purchase_product_id = products.product_id) AS "product_purchase_count" FROM "products" AS "products" WHERE ("products"."deletedAt" IS NULL) LIMIT 5 OFFSET 5) AS "products" LEFT OUTER JOIN "main_courses" AS "main_course" ON "products"."product_id" = "main_course"."main_product_id" AND ("main_course"."deletedAt" IS NULL) LEFT OUTER JOIN "side_dishes" AS "side_dish" ON "products"."product_id" = "side_dish"."side_product_id" AND ("side_dish"."deletedAt" IS NULL) LEFT OUTER JOIN "drinks" AS "drink" ON "products"."product_id" = "drink"."drink_product_id" AND ("drink"."deletedAt" IS NULL) LEFT OUTER JOIN "product_purchases" AS "product_purchases" ON "products"."product_id" = "product_purchases"."purchase_product_id" AND ("product_purchases"."deletedAt" IS NULL AND "product_purchases"."createdAt" BETWEEN '2022-05-06 08:35:12.577 +00:00' AND '2022-05-08 08:35:12.577 +00:00');
According to pgadmin the problem starts when doing the join on main_courses
ERROR: column reference "product_id" is ambiguous
LINE 1: ...EFT OUTER JOIN "main_courses" AS "main_course" ON "products"...
I seem to not understand how the sequelize join works as I am using the ORM method of not having any columns be the same. The foreign keys are all different from 'product_id'. Is product_id getting created and joined again on the aggregation possibly? Why am I getting this error, and how can I achieve the result of getting the count of the product_purchases along with the joined array of purchased products.
Obviously I could just get the purchased_products.length and then add it to the object itself, but I'm trying to figure out if you can do it through sequelize without having to hack it in.

Joining same table multiple times with Sequelize

I have the following models:
const User = Sequelize.define('user', {
login: Sequelize.DataTypes.STRING,
password: Sequelize.DataTypes.STRING,
is_manager: Sequelize.DataTypes.BOOLEAN,
notes: Sequelize.DataTypes.STRING
});
const Bike = Sequelize.define('bike', {
model: Sequelize.DataTypes.STRING,
photo: Sequelize.DataTypes.BLOB,
color: Sequelize.DataTypes.STRING,
weight: Sequelize.DataTypes.FLOAT,
location: Sequelize.DataTypes.STRING,
is_available: Sequelize.DataTypes.BOOLEAN
});
const Rate = Sequelize.define('rate', {
rate: Sequelize.DataTypes.INTEGER
});
Rate.belongsTo(User);
User.hasMany(Rate);
Rate.belongsTo(Bike);
Bike.hasMany(Rate);
And I'd like to select bikes with their average rates, plus rates of the current user for each bike:
Bike.findAll({
attributes: {include: [[Sequelize.fn('AVG', Sequelize.col('rates.rate')), 'rate_avg']],
},
include: [{
model: Rate,
attributes: []
}, {
model: Rate,
attributes: ['rate'],
include: [{
model: User,
attributes: [],
where: {
login: req.user.login
}
}]
}],
group: Object.keys(Bike.rawAttributes).map(key => 'bike.' + key) // group by all fields of Bike model
})
It constructs the following query: SELECT [bike].[id], [bike].[model], [bike].[photo], [bike].[color], [bike].[weight], [bike].[location], [bike].[is_available], AVG([rates].[rate]) AS [rate_avg], [rates].[id] AS [rates.id], [rates].[rate] AS [rates.rate] FROM [bikes] AS [bike] LEFT OUTER JOIN [rates] AS [rates] ON [bike].[id] = [rates].[bikeId] LEFT OUTER JOIN ( [rates] AS [rates] INNER JOIN [users] AS [rates->user] ON [rates].[userId] = [rates->user].[id] AND [rates->user].[login] = N'user' ) ON [bike].[id] = [rates].[bikeId] GROUP BY [bike].[id], [bike].[model], [bike].[photo], [bike].[color], [bike].[weight], [bike].[location], [bike].[is_available];
And fails: SequelizeDatabaseError: The correlation name 'rates' is specified multiple times in a FROM clause.
How do I write the query right? I need Sequelize to assign another alias to the rates table used in the 2nd join (and add its columns to the GROUP BY clause, but that's the next step).
You can do multiple inner joins with same table by adding extra same association with that model but with a different alias that is as: 'alias1' , as: 'alias2' ,... - all this existing with the same model + same type of association.
Also posted this solution at github issue: https://github.com/sequelize/sequelize/issues/7754#issuecomment-783404779
E.g. for Chats that have many Receiver
Associations (Duplicating for as many needed)
Chat.hasMany(Receiver, {
// foreignKey: ...
as: 'chatReceiver',
});
Chat.hasMany(Receiver, {
// foreignKey: ...
as: 'chatReceiver2',
});
Now you are left to include associated model multiple times all with different alias so it does not gets overridden.
So you can use them in query as below:
Chat.findAll({
attributes: ["id"],
include: [{
required: true,
model: Receiver,
as: 'chatReceiver', // Alias 1
attributes: [],
where: { userID: 1 }, // condition 1
}, {
required: true,
model: Receiver,
as: 'chatReceiver2', // Alias 2
attributes: [],
where: { userID: 2 }, // condition 2 as needed
}]
});
Solution :
Bike.findAll({
attributes: {include: [[Sequelize.fn('AVG', Sequelize.col('rates.rate')), 'rate_avg']],
},
include: [{
model: Rate,
attributes: []
}, {
model: Rate,
required : false , // 1. just to make sure not making inner join
separate : true , // 2. will run query separately , so your issue will be solved of multiple times
attributes: ['rate'],
include: [{
model: User,
attributes: [],
where: {
login: req.user.login
}
}]
group : [] // 3. <------- This needs to be managed , so please check errors and add fields as per error
}],
group: Object.keys(Bike.rawAttributes).map(key => 'bike.' + key) // group by all fields of Bike model
})
NOTE : READ THE COMMENTS
Sequelize doesn't support including through the same association twice (see here, here, and here). At the model level, you can define 2 different associations between Bike and Rate, but having to change the model, adding new foreign keys etc, is a very hacky solution.
Incidentally, it wouldn't solve your other problem, which is that you're grouping only by Bike but then want to select the user's rate. To fix that, you'd also have to change your grouping to include the user rates. (Note that if a user has more than 1 rate per bike, that might also create some inefficiency, as the rates for the bike are averaged repeatedly for each of the user's rates.)
A proper solution would be using window functions, first averaging the rates per bike and then filtering out all the rates not belonging to the logged in user. Might look something like this:
SELECT *
FROM (
SELECT bike.*,
users.login AS user_login,
AVG (rates.rate) OVER (PARTITION BY bike.id) AS rate_avg
FROM bike
INNER JOIN rates ON rates.bikeId = bike.id
INNER JOIN users ON rates.userId = users.id
)
WHERE user_login = :req_user_login
Unfortunately, as far as I'm aware sequelize doesn't currently support subqueries in the FROM clause and using window functions in this way, so you'd have to fall back to a raw query.

Sequelize join twice on two tables

I have two table: Conversations and ConversationParticipants.
I need to get the list of conversations that both users 1 and 2 participate too.
In MySQL, the query would be this:
SELECT conversation_participants.conversation_id FROM conversation_participants
JOIN conversations t1
ON t1.conversation_id = conversation_participants.conversation_id
AND conversation_participants.user_id = 11
JOIN conversation_participants t2
ON t1.conversation_id = t2.conversation_id
AND t2.user_id = 2
However, in Sequelize I cannot understand how to set models relations so that I can make a single query. I tried this without success (please note that this is almost pseudo code, reported as such for clarity):
var Conversation = sequelize.define('conversations', {
conversationId: Sequelize.INTEGER.UNSIGNED,
});
var ConversationParticipant = sequelize.define('conversation_participants', {
participationId: Sequelize.INTEGER.UNSIGNED,
userId: Sequelize.INTEGER.UNSIGNED,
conversationId : Sequelize.INTEGER.UNSIGNED,
});
Conversation.hasMany(ConversationParticipant, { as : 'Participants'});
and then
ConversationParticipant.findAll({
where : { userId : 1 },
include : [{
model : Conversation,
as : 'conversation',
include : [{
model : ConversationParticipant,
as : 'Participants',
where : { userId : 2 }
}]
}]
I get the following error:
Error: conversation_participants is not associated with conversations!.
Any idea?
One way you could do it is find the conversations that have participants with user 1 or 2, and afterwards filter the conversations that have both of them:
const userIdsFilter = [1,2];
//find Conversations that have user 1 OR user 2 as participants,
Conversation.findAll({
include : [{
model : ConversationParticipant,
as : 'conversation_participants',
where : { userId : { $in: userIdsFilter }}
}]
}).then((conversations) =>
{
//filter the conversations that have the same length participants
//as the userIdsFilter length (that way it excludes the ones
//that have just one of the users as participants)
return conversations.filter((conversation) =>
conversation.conversation_participants.length === userIdsFilter.length);
}).then((conversations) =>
{
//do what you need with conversations..
})
You are missing the belongsTo association in the ConversationParticipant definition. Sequelize model needs explicit association since you are trying to access Conversation via the ConversationParticipant instance even though Conversation is associated ConversationParticipant is not.
Something like this in addition to hasMany association:
ConversationParticipant.belongsTo(Conversation, { as : 'conversation',
foreignKey: 'conversationId', targetKey: 'conversationId'});
And then remove the conversationId field definition from ConversationParticipant model since belongsTo also creates that for you.

Use JOIN in Sequelize

prompt how to use JOIN in Sequelize ?
I have the following code:
db.User.findAll({
attributes: ['id'],
where: {vipEnd: {lt: expiresVip}, vip: true},
limit: 100,
order: 'id'
}).then(function(users) {
In terms of 4, it looks like this:
SELECT "vkId" AS "id"
FROM "user" AS "user"
WHERE "user"."vipEnd" < 1469699683 --expiresVip
AND "user"."vip" = true
ORDER BY id
LIMIT '100';
How to achieve such a result?
SELECT "vkId" AS "id"
FROM "user" AS "user"
LEFT JOIN "notification" ON "notification"."userId" = "user"."vkId"
WHERE "user"."vipEnd" < 1469699683
AND "user"."vip" = true
AND "notification"."type" = 4
AND "notification"."expiresVipDate" < 1469699683
ORDER BY id
LIMIT '100';
Thank you!
You need to define association for models.
ex:
db.User.hasMany(db.Notification, {foreignKey: 'UserId'});
use properties include in sequelize:
db.User.findAll({
include: [{
model: db.Notification,
required: true//true INNER JOIN, false LEFT OUTER JOIN - default LEFT OUTER JOIN
}]}).then(function(user) {}, function(err) {
res.json(err, 400);});
You should consult more here
http://docs.sequelizejs.com/en/latest/docs/associations/

Sequelize belongsTo and include

I am having a hard time making use of BelongsTo relationship in a query.
Here is the relationship:
models.Area.hasMany(models.Airport);
models.Airport.belongsTo(models.Area, {foreignKey: 'areaId', as: 'area'});
Now I try to run a query:
models.Airport
.findAll( {
where: { active: 1 },
include: { model: models.Area }
})
I get an error:
Error: area is not associated to airport!
What am I doing wrong?
I figured it out. I can't say I fully understand the problem, but here it is.
When creating the relationship, I use as: 'area'.
The result is, I need to add as: 'area' to the include statement:
models.Airport
.findAll( {
where: { active: 1 },
include: { model: models.Area, as: 'area' }
})
Once the two match, things go better.

Resources