Lets say I have 2 models, User and Event. They are set up as am:m relationship User.belongsToMany(Event)
and Event.belongsToMany(User)
. This all works fine, but when I do User.findAndCountAll({include: [{model: Event}]})
, the resulting json for result.rows looks like
[
...
{
userdata...,
events: [ // included by model relation
...included event data...,
junction_table: { // I dont want this data sent back to client, just above data
...data i dont want...
}
}
...
]
I want
[
{
userdata...,
events: [
{ included event data without junction_table }
]
}
]
Is there a way to strip out the junction_table that for some reason is automatically included with every single event without .mapping over the data and rebuilding it? I'd rather not send all this data I don't need over the wire to the clients all the time. I tried only specifying attributes: []
on the model, but it won't go away.
This can be achieved by specifying that you don't want the "through" table attributes, according to this SO question .
In your case, it would be :
User.findAndCountAll({
include:
[{model: Event, through: { attributes: [] }}]
});
SELECT *
FROM a
JOIN b ON EXISTS
( SELECT * FROM junction j
WHERE j.a_id = a.a_id
AND j.b_id = b.b_id
);
This only works if the junction table is unique on {a_id , b_id}
, with a_id
, b_id
being the primary keys of a
and b
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.