how to add where condition in file schema.prisma
- User
how to add where condition in file schema.prisma
- Assistant
In order to add a where condition to a file schema in Prisma, you can use the
@defaultdirective 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
Usermodel and aPostmodel, with a one-to-many relationship between them using the@relationdirective. We've also added a@defaultdirective to theUsermodel, 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
@defaultdirective to include other types of filters, such asAND,OR, andNOT. 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".