with clause type not being inferred properly on insert

I have to assert it as a number for the type error to go away:
const countryIdQuery = db.$with('country_id').as(
db
.select({
id: countries.id,
})
.from(countries)
.where(eq(countries.code, data.countryCode)),
);

const [address] = await db
.with(countryIdQuery)
.insert(addresses)
.values({
addressLine1: data.addressLine1,
addressLine2: data.addressLine2,
city: data.city,
state: data.state,
postalCode: data.postalCode,
countryId: Number(countryIdQuery.id),
})
.returning({ id: addresses.id });

return address;
const countryIdQuery = db.$with('country_id').as(
db
.select({
id: countries.id,
})
.from(countries)
.where(eq(countries.code, data.countryCode)),
);

const [address] = await db
.with(countryIdQuery)
.insert(addresses)
.values({
addressLine1: data.addressLine1,
addressLine2: data.addressLine2,
city: data.city,
state: data.state,
postalCode: data.postalCode,
countryId: Number(countryIdQuery.id),
})
.returning({ id: addresses.id });

return address;
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server