Have I got inserts right for MySQL(Planetscale)?

I'm switching over to Drizzle from Prisma, just wanted to make sure I have inserts written correctly, specifically inserts with relationships, for example, this is inserting a business with an associated businessAddress, does that look right? Ctx is just TRPC.
await ctx.db.transaction(async (tx) => {
await tx.insert(businessTable).values({
businessName: input.businessName,
description: input.description,
email: input.email,
hasAgreedTerms: input.hasAgreedTerms,
userId: ctx.userId
});

const business = await tx.select({ businessId: businessTable.businessId }).from(businessTable).where(eq(businessTable.userId, ctx.userId));

const businessId = business[0]?.businessId;

await tx.insert(businessAddressTable).values({
addressOne: input.addressOne,
addressTwo: input.addressTwo,
city: input.city,
postCode: input.postCode,
countryId: input.countryId,
businessId: businessId
});
});
await ctx.db.transaction(async (tx) => {
await tx.insert(businessTable).values({
businessName: input.businessName,
description: input.description,
email: input.email,
hasAgreedTerms: input.hasAgreedTerms,
userId: ctx.userId
});

const business = await tx.select({ businessId: businessTable.businessId }).from(businessTable).where(eq(businessTable.userId, ctx.userId));

const businessId = business[0]?.businessId;

await tx.insert(businessAddressTable).values({
addressOne: input.addressOne,
addressTwo: input.addressTwo,
city: input.city,
postCode: input.postCode,
countryId: input.countryId,
businessId: businessId
});
});
Appreciate the input, thanks.
1 Reply
Angelelz
Angelelz11mo ago
If businessId is anything but an auto_increment, then this is correct If it was an autoincrement field, then you can get it back from the insert query result
Want results from more Discord servers?
Add your server