Subqueries with INSERT - errors

I'm trying to create an insert that uses a subquery
const sq = db.select({id: orgs.id}).from(orgs).where(eq(orgs.publicId, input.orgPublicId)).as('sq');
const insertOrgResponse = await db.insert(orgInvitations).values({
orgId: sq.id,
invitedByUserId: userId,
publicId: newPublicId,
role: input.role,
inviteToken: nanoid(),
})
const sq = db.select({id: orgs.id}).from(orgs).where(eq(orgs.publicId, input.orgPublicId)).as('sq');
const insertOrgResponse = await db.insert(orgInvitations).values({
orgId: sq.id,
invitedByUserId: userId,
publicId: newPublicId,
role: input.role,
inviteToken: nanoid(),
})
Getting type errors on orgId, seems its not detecting correct type of sq.id If I explicitly set sq.id to a number, no more errors Still need to queries though....
3 Replies
TravisFrank
TravisFrank14mo ago
@McPizza I'm trying to do the same, did you ever find a solution?
McPizza
McPizzaOP14mo ago
Never got this to work for me
Angelelz
Angelelz14mo ago
I do not think this is supported yet. The best workaround is run a select and the an update inside a transaction
Want results from more Discord servers?
Add your server