hexcowboy
hexcowboy
Explore posts from servers
DTDrizzle Team
Created by hexcowboy on 1/24/2024 in #help
not getting proper type inference on any queries
i'm trying to get jobs with companies using drizzle queries
const job = await db.query.jobs.findFirst({
where: eq(jobs.id, id),
with: {
company: true,
},
});

const title = `${job.title} at ${job.company.name}`;
const job = await db.query.jobs.findFirst({
where: eq(jobs.id, id),
with: {
company: true,
},
});

const title = `${job.title} at ${job.company.name}`;
but for some reason the type of jobs does not have the company property: Property 'company' does not exist on type '...' i have made sure to define the relation in my schema file
export const jobs = pgTable("jobs", {
id: uuid("id").notNull().primaryKey().defaultRandom(),
companyId: uuid("company_id")
.references(() => companies.id, { onDelete: "cascade" })
.notNull(),
/* ... */
});

export const jobCompanyRelation = relations(jobs, ({ one }) => ({
company: one(companies, {
fields: [jobs.companyId],
references: [companies.id],
}),
}));
export const jobs = pgTable("jobs", {
id: uuid("id").notNull().primaryKey().defaultRandom(),
companyId: uuid("company_id")
.references(() => companies.id, { onDelete: "cascade" })
.notNull(),
/* ... */
});

export const jobCompanyRelation = relations(jobs, ({ one }) => ({
company: one(companies, {
fields: [jobs.companyId],
references: [companies.id],
}),
}));
using drizzle-orm 0.29.3
2 replies
DTDrizzle Team
Created by hexcowboy on 1/16/2024 in #help
multiple joins of same table in one query?
i want to do this query
return await db
.select({
...getTableColumns(interests),
user: getTableColumns(users),
referral: getTableColumns(referrals),
referrer: getTableColumns(users),
})
.from(interests)
.innerJoin(users, eq(interests.userId, users.id))
.leftJoin(referrals, eq(interests.referralId, referrals.id))
.leftJoin(users, eq(referrals.referrer, users.id));
return await db
.select({
...getTableColumns(interests),
user: getTableColumns(users),
referral: getTableColumns(referrals),
referrer: getTableColumns(users),
})
.from(interests)
.innerJoin(users, eq(interests.userId, users.id))
.leftJoin(referrals, eq(interests.referralId, referrals.id))
.leftJoin(users, eq(referrals.referrer, users.id));
basically i have one inner join on users and also a left join on users. but the resulting type makes all fields never when i remove either of the joins on users, i get the correct types
3 replies