Transactions & Raw SQL

Preview — 3 of 10 questions

What is the difference between prisma.$transaction([...]) and prisma.$transaction(async (tx) => {})?

ASequential transactions are faster; interactive transactions support rollback while sequential do not
BSequential ($transaction([op1, op2])) runs all operations atomically but cannot use the result of one operation as input to the next; interactive ($transaction(async (tx) => {})) allows chaining results between operations within a single transaction using the tx client
CInteractive transactions are deprecated in Prisma 5; use sequential only
DThey are identical in behavior; the syntax difference is cosmetic

Why should you use Prisma.sql template tag instead of $queryRawUnsafe for raw SQL queries?

APrisma.sql is faster because it bypasses query parsing
BPrisma.sql automatically parameterizes interpolated values, preventing SQL injection; $queryRawUnsafe directly interpolates strings into the query without sanitization, creating SQL injection vulnerabilities
C$queryRawUnsafe is the only way to use raw SQL in Prisma; Prisma.sql is for TypeScript type generation only
DThey are identical; Unsafe in the name is misleading and refers to type safety, not SQL injection

How does the N+1 problem manifest in Prisma and how is it fixed?

AN+1 occurs when you use findMany with take: N+1; fix by using take: N
BN+1 is a Prisma-specific bug fixed in version 4; no action needed
CN+1 occurs when you fetch N parent records then issue one query per record to fetch related data in a loop; fix by using include in the initial query to fetch relations in a single JOIN
DN+1 occurs only with many-to-many relations; fix with explicit join models

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.