title: How to do SQL insert for Postgres using Prisma with condition on count of rows?
body: <p>I am trying to insert a row into database provided that - number of rows satisfying some condition already in the table is less than certain threshold, say 10. For example, in below model, I don't want to have a project to have more than 10 users; so <code>count(projectId)</code> should be less than 10:</p>
  <pre><code>model User {
    id            BigInt      @id @default(autoincrement())
  
    firstName     String      @map(&quot;first_name&quot;)
    lastName      String      @map(&quot;last_name&quot;)
    email         String      @unique
  
    password      String
    passwordHash  String      @map(&quot;password_hash&quot;)
  
    createdAt     DateTime    @db.Timestamptz() @default(now()) @map(&quot;created_at&quot;)
    updatedAt     DateTime    @db.Timestamptz() @updatedAt @map(&quot;updated_at&quot;)
  
    project       Project     @relation(fields: [projectId], references: [id])
    projectId     BigInt?     @map(&quot;project_id&quot;)
  
    @@map(&quot;app_user&quot;)
  }
  
  model Project {
    id            BigInt    @id @default(autoincrement())
    name          String
  
    users         User[]
  
    @@map(&quot;project&quot;)
  }
  </code></pre>
  <p>In general SQL world, I would rely on transaction with <strong>Optimistic Concurrency Control</strong> and then attempt the insert only after reading the count of rows matching <code>project_id</code>. Since, Prisma doesn't provide traditional long running transaction, I am stuck. I cannot just simply run the count query first and then do the insert since it won't be atomic in nature.</p>
  <p>How to handle this scenario with Prisma?</p>