Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

how to add where condition in file schema.prisma

  1. User

    how to add where condition in file schema.prisma

  2. Assistant

    In order to add a where condition to a file schema in Prisma, you can use the @default directive with a function that returns a filter object. Here's an example:

    model User {
      id Int @id @default(autoincrement())
      name String
      email String @unique
      
      posts Post[] @relation("UserPosts")
      
      @@map("users")
      @@default(
        select: {
          email_not: ""
        }
      )
    }
    
    model Post {
      id Int @id @default(autoincrement())
      title String
      content String
      
      author User @relation("UserPosts", fields: [userId], references: [id])
      userId Int
      
      @@map("posts")
    }
    

    In this example, we have a User model and a Post model, with a one-to-many relationship between them using the @relation directive. We've also added a @default directive to the User model, which specifies a filter object that will be applied to all queries for this model. In this case, the filter is { email_not: "" }, which means that only users with non-empty email addresses will be returned.

    You can modify the @default directive to include other types of filters, such as AND, OR, and NOT. For example:

    @@default(
        select: {
          AND: [
            { email_not: "" },
            { posts_some: { title_contains: "Prisma" } }
          ]
        }
      )
    

    This filter will return only users who have non-empty email addresses and who have at least one post with a title that includes the string "Prisma".