Hey guys, need some help writing a query in one tr...
# orm-help
m
Hey guys, need some help writing a query in one transaction. I could create this in 2 separate transactions, but can't use the $transaction API as I need the ID of the first query, so trying to figure out how its done in one. See below for a high level example, where treatment_city is a joiner table for the two entities, city and treatment - when I insert a treatment, I want to insert the treatment ID and one other field into the joiner table along with the cityIds passed through to the API
Copy code
await prisma.treatment.create({
	data: {
		name: "Treatment A",
		clinic: { connect: { id: "SOME ID" }},
		metadata: { create: { infos: "JSON" }},
		treatment_city: {
			createMany: {
				data: cityIds.map(cityId => ({
					city_id: cityId,
					metadata_id: <Id from above>, (??)
					treatment_id: <id of created treatment>, (??)
				}))
			}
		}
	}
})
d
you can try using the interactiveTransaction feature. It is currently in preview but we are using it in production and it is quite stable
💯 1
❤️ 1
m
Thanks @Dominic Hadfield - but I now get "promises is not iterable" when using Interactive Transaction
Around, I'm assuming the cityIds map
Copy code
await prisma.$transaction(async () => {
		const treatment = await prisma.treatment.create({
			data: {
				name: name,
				clinic: { connect: { id: "SOME ID" } },
				metadata: {
					create: {
						infos: JSON.stringify("SOME JSON"),
					},
				},
			},
		});

		await prisma.treatment_city.createMany({
			data: cityIds.map((cityId) => ({
				city_id: cityId,
				metadata_id: treatment.metadata.id,
				treatment_id: treatment.id,
			})),
		});
	});
d
You don't need to await the last query and should return it
Let me double check
m
I did try that also with the same result
Feel like it might be a bug - is there an alternative solution to this?