KDOC 582: Prismaでネイティブupsertが使われる条件を見る
この文書のステータス
- 作成
- 2026-05-14 貴島
- レビュー
- <署名>
概要
JavaScriptのORMであるPrismaには upsert() 関数がある。これがSQLレベルで upsert(INSERT ... ON CONFLICT DO UPDATE) になるには条件があり、満たされない場合は SELECT + INSERT/UPDATE になる。2クエリなので、並列実行したときにタイミングによって競合する可能性がある。2つの実行クエリの両方がSELECTを通過すると、どちらもINSERTになり、ユニーク制約があればエラーになる。
Prisma Client uses a database upsert for an upsert query when the query meets the following criteria:
- There are no nested queries in the upsert’s create and update options
- The query does not include a selection that uses a nested read
- The query modifies only one model
- There is only one unique field in the upsert’s where option
- The unique field in the where option and the unique field in the create option have the same value
関連
なし。