Why does drizzle want to request the same column twice from db?

So basically this is my function:
async getUserByToken({ token }: { token: string }) {
const sq = this.db
.select()
.from(bansTable)
.where(gt(bansTable.issuedUntil, dayjs().toDate()))
.as("sq");

const data = await this.db
.select({
id: sessionsTable.id,
token: sessionsTable.token,
refreshToken: sessionsTable.refreshToken,
rejectedAt: sessionsTable.rejectedAt,
createdAt: sessionsTable.createdAt,
user: jsonBuildObject({
id: usersTable.id,
username: usersTable.username,
discordUsername: usersTable.discordUsername,
discordId: usersTable.discordId,
createdAt: usersTable.createdAt,
email: usersTable.email,
}),
bans: jsonAggBuildObject({
id: sq.id,
issuedTo: sq.issuedTo,
issuedUntil: sq.issuedUntil,
issuedBy: sq.issuedBy,
reason: sq.reason,
issuedAt: sq.issuedAt,
}),
})
.from(sessionsTable)
.leftJoin(usersTable, eq(sessionsTable.userId, usersTable.id))
.leftJoin(sq, eq(usersTable.id, sq.issuedTo))
.where(eq(sessionsTable.token, token))
.groupBy(usersTable.id, sessionsTable.id);
return takeFirst(data);
}
async getUserByToken({ token }: { token: string }) {
const sq = this.db
.select()
.from(bansTable)
.where(gt(bansTable.issuedUntil, dayjs().toDate()))
.as("sq");

const data = await this.db
.select({
id: sessionsTable.id,
token: sessionsTable.token,
refreshToken: sessionsTable.refreshToken,
rejectedAt: sessionsTable.rejectedAt,
createdAt: sessionsTable.createdAt,
user: jsonBuildObject({
id: usersTable.id,
username: usersTable.username,
discordUsername: usersTable.discordUsername,
discordId: usersTable.discordId,
createdAt: usersTable.createdAt,
email: usersTable.email,
}),
bans: jsonAggBuildObject({
id: sq.id,
issuedTo: sq.issuedTo,
issuedUntil: sq.issuedUntil,
issuedBy: sq.issuedBy,
reason: sq.reason,
issuedAt: sq.issuedAt,
}),
})
.from(sessionsTable)
.leftJoin(usersTable, eq(sessionsTable.userId, usersTable.id))
.leftJoin(sq, eq(usersTable.id, sq.issuedTo))
.where(eq(sessionsTable.token, token))
.groupBy(usersTable.id, sessionsTable.id);
return takeFirst(data);
}
and what I get from logger is this query
2 Replies
Blitz
BlitzOP3w ago
Query: select "sessions"."id", "sessions"."token", "sessions"."refresh_token", "sessions"."rejected_at", "sessions"."created_at", json_build_object('id',"users"."id",'username',"users"."username",'discordUsername',"users"."discord_username",'discordId',"users"."discord_id",'createdAt',"users"."created_at",'email',"users"."email"), coalesce(
json_agg(json_build_object('id',"sq"."id",'issuedTo',"sq"."issued_to",'issuedUntil',"sq"."issued_at",'issuedBy',"sq"."issued_by",'reason',"sq"."reason",'issuedAt',"sq"."issued_at")
)
FILTER (WHERE "sq"."id" IS NOT NULL AND "sq"."issued_to" IS NOT NULL AND "sq"."issued_at" IS NOT NULL AND "sq"."issued_by" IS NOT NULL AND "sq"."reason" IS NOT NULL AND "sq"."issued_at" IS NOT NULL)
,'[]') from "sessions" left join "users" on "sessions"."user_id" = "users"."id" left join (select "id", "issued_at", "issued_at", "issued_by", "issued_to", "reason" from "bans" where "bans"."issued_at" > $1) "sq" on "users"."id" = "sq"."issued_to" where "sessions"."token" = $2 group by "users"."id", "sessions"."id"
Query: select "sessions"."id", "sessions"."token", "sessions"."refresh_token", "sessions"."rejected_at", "sessions"."created_at", json_build_object('id',"users"."id",'username',"users"."username",'discordUsername',"users"."discord_username",'discordId',"users"."discord_id",'createdAt',"users"."created_at",'email',"users"."email"), coalesce(
json_agg(json_build_object('id',"sq"."id",'issuedTo',"sq"."issued_to",'issuedUntil',"sq"."issued_at",'issuedBy',"sq"."issued_by",'reason',"sq"."reason",'issuedAt',"sq"."issued_at")
)
FILTER (WHERE "sq"."id" IS NOT NULL AND "sq"."issued_to" IS NOT NULL AND "sq"."issued_at" IS NOT NULL AND "sq"."issued_by" IS NOT NULL AND "sq"."reason" IS NOT NULL AND "sq"."issued_at" IS NOT NULL)
,'[]') from "sessions" left join "users" on "sessions"."user_id" = "users"."id" left join (select "id", "issued_at", "issued_at", "issued_by", "issued_to", "reason" from "bans" where "bans"."issued_at" > $1) "sq" on "users"."id" = "sq"."issued_to" where "sessions"."token" = $2 group by "users"."id", "sessions"."id"
Take a look how left join request "issued_at" two times Why is that happening?
Angelelz
Angelelz3w ago
Check your table definition for bansTable you might have two columns that although they look to have different name, the database name might be issues_at When I say the database name, I mean the string you pass as the first parameter to the column definition
Want results from more Discord servers?
Add your server