I have two separate tables CourseOffer
and CourseAddon
where CourseAddon
has foreignKey courseOfferId
in CourseAddon
table.
Now I wish to add a new column in my CourseOffer
table named alternateCourseOfferId
which in itself is linked to another CourseOffer
.
Table "public.CourseOffer"
Column | Type | Collation | Nullable | Default
----------------+--------------------------+-----------+----------+-------------------------------------------
id | integer | | not null | nextval('"CourseOffer_id_seq"'::regclass)
title | character varying(255) | | |
description | text | | |
courseId | integer | | not null |
fee | numeric(10,2) | | not null |
discountedFee | numeric(10,2) | | |
email | character varying(255) | | |
phone | character varying(255) | | |
expiryAt | timestamp with time zone | | |
durationInDays | integer | | |
offerExpiryAt | timestamp with time zone | | |
offerStartedAt | timestamp with time zone | | |
admin_user_id | integer | | |
hidden | boolean | | not null | false
position | integer | | |
createdAt | timestamp with time zone | | not null | CURRENT_TIMESTAMP
updatedAt | timestamp with time zone | | not null | CURRENT_TIMESTAMP
actualCourseId | integer | | |
accepted | boolean | | | false
hasShipment | boolean | | | false
allowCoupon | boolean | | | false
allowAddon | boolean | | | false
Table "public.CourseAddon"
Column | Type | Collation | Nullable | Default
--------------------+-----------------------------+-----------+----------+-------------------------------------------
id | integer | | not null | nextval('"CourseAddon_id_seq"'::regclass)
courseId | integer | | |
addonCourseId | integer | | |
createdAt | timestamp without time zone | | | CURRENT_TIMESTAMP
updatedAt | timestamp without time zone | | | CURRENT_TIMESTAMP
courseOfferId | integer | | |
addonCourseOfferId | integer | | |
seqId | integer | | |
I am having a Sequelize relation in CourseOffer
which finds all associated CourseAddon
.
CourseOffer.Addons = CourseOffer.hasMany(CourseAddon, {
foreignKey: 'courseOfferId',
scope: (findOptions) => ({
courseOfferId: {$ne: null},
$and: Model.literal('"CourseAddon"."courseId" = (SELECT "courseId" from "CourseOffer" where "CourseOffer"."id" = "courseOfferId")'),
}),
as: 'addons',
onUpdate: 'cascade',
onDelete: 'cascade',
});
I wish that if alternateCourseOfferId
exists in a CourseOffer
, it should look via the value of alternateCourseOfferId
in place of id
as foreignKey
.
How to modify the Sequelize query to achieve these results?
I am expecting if a CourseOffer
has alternateCourseOfferId
, it should search via alternateCourseOfferId
in place of id
over CourseAddon
table.
Yash Garg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.