I am working with a model A that has a 1:N association with a model B. My objective is to retrieve all records from A and determine whether there is at least one associated record from B (true) or not (false).
The relationship setup:
ModelA.hasMany(ModelB)
ModelB.belongsTo(ModelA)
The query I currently have is:
const results = await ModelA.findAll({
include: { model: ModelB, required: false }
})
This allows me to retrieve matching records from B when they exist due to the outer join, but my main concern is simply to identify if any records from B are present. Is there a way to customize the SELECT query to directly return true/false based on the result of the join?
My desired custom query would be:
SELECT a.*, CASE WHEN b.id IS NOT NULL THEN true ELSE false END
FROM modelA a LEFT OUTER JOIN modelB b on b.model_a_id = a.id