$count with relation api returning wrong values ( 2 instead of 0)
output:
project 1 >> { id: 1, matches: [], matchesCount: '2' }expected
project 1 >> { id: 1, matches: [], matchesCount: '0' } project 1 >> { id: 1, matches: [], matchesCount: '2' } project 1 >> { id: 1, matches: [], matchesCount: '0' }async function findProjectById(id: number) {
return await db.query.projects.findFirst({
where: eq(projects.id, id),
with: {
...
matches:true
},
extras: {
matchesCount: db
.$count(matches, eq(matches.projectId, projects.id))
.as("matchesCount"),
},
});