trying to use $count withing query relational api

PostgresError: column projects.project_id does not exist
const projectsInQueueWithDrizzle = await db.query.projects.findMany({
with: {
matches: true,
},
extras: ({ id: projectId }) => ({
matchCount: db
.$count(matches, eq(matches.projectId, projectId))
.as("matchCount"),
}),
});
const projectsInQueueWithDrizzle = await db.query.projects.findMany({
with: {
matches: true,
},
extras: ({ id: projectId }) => ({
matchCount: db
.$count(matches, eq(matches.projectId, projectId))
.as("matchCount"),
}),
});
1 Reply
uber
uberOP4w ago
generated query:
Query: select "projects"."id", "projects"."client_id", "projects"."status", "projects"."errors", "projects"."service_id", "projects"."contact", "projects"."city_id", "projects"."address", "projects"."licensesAnswers", "projects"."form_data", (select count(*) from "matches" where "projects"."project_id" = "projects"."id") as "matchCount", "projects_matches"."data" as "matches" from "projects" left join lateral (select coalesce(json_agg(json_build_array("projects_matches"."id", "projects_matches"."project_id", "projects_matches"."expert_id")), '[]'::json) as "data" from "matches" "projects_matches" where "projects_matches"."project_id" = "projects"."id") "projects_matches" on true
Query: select "projects"."id", "projects"."client_id", "projects"."status", "projects"."errors", "projects"."service_id", "projects"."contact", "projects"."city_id", "projects"."address", "projects"."licensesAnswers", "projects"."form_data", (select count(*) from "matches" where "projects"."project_id" = "projects"."id") as "matchCount", "projects_matches"."data" as "matches" from "projects" left join lateral (select coalesce(json_agg(json_build_array("projects_matches"."id", "projects_matches"."project_id", "projects_matches"."expert_id")), '[]'::json) as "data" from "matches" "projects_matches" where "projects_matches"."project_id" = "projects"."id") "projects_matches" on true
why is it using projects.project_id ive also tried
extras: ({ id: projectId }) => ({
matchCount: db
.$count(matches, eq(matches.projectId, projects.id))
.as("matchCount"),
}),
extras: ({ id: projectId }) => ({
matchCount: db
.$count(matches, eq(matches.projectId, projects.id))
.as("matchCount"),
}),
same error I can fix it if i rename my project table column name from "id" to id: serial("project_id").primaryKey(), anyone got a better solution?

Did you find this page helpful?