Skip to main content

Null and undefined

Prisma Client differentiates between null and undefined:

  • null is a value
  • undefined means do nothing
info

The data below represents a User table. This set of data will be used in all of the examples below:

idnameemail
1Nikolas[email protected]
2Martin[email protected]
3empty[email protected]
4Tyler[email protected]

null and undefined in queries that affect many records

This section will cover how undefined and null values affect the behavior of queries that interact with or create multiple records in a database.

Null

Consider the following Prisma Client query which searches for all users whose name value matches the provided null value:

const users = await prisma.user.findMany({
where: {
name: null,
},
})
Show query results

Because null was provided as the filter for the name column, Prisma Client will generate a query that searches for all records in the User table whose name column is empty.

Undefined

Now consider the scenario where you run the same query with undefined as the filter value on the name column:

const users = await prisma.user.findMany({
where: {
name: undefined,
},
})
Show query results

Using undefined as a value in a filter essentially tells Prisma Client you have decided not to define a filter for that column.

An equivalent way to write the above query would be:

const users = await prisma.user.findMany()

This query will select every row from the User table.

info

Note: Using undefined as the value of any key in a Prisma Client query's parameter object will cause Prisma ORM to act as if that key was not provided at all.

Although this section's examples focused on the findMany function, the same concepts apply to any function that can affect multiple records, such as updateMany and deleteMany.

null and undefined in queries that affect one record

This section will cover how undefined and null values affect the behavior of queries that interact with or create a single record in a database.

warning

Note: null is not a valid filter value in a findUnique query.

The query behavior when using null and undefined in the filter criteria of a query that affects a single record is very similar to the behaviors described in the previous section.

Null

Consider the following query where null is used to filter the name column:

const user = await prisma.user.findFirst({
where: {
name: null,
},
})
Show query results

Because null was used as the filter on the name column, Prisma Client will generate a query that searches for the first record in the User table whose name value is empty.

Undefined

If undefined is used as the filter value on the name column instead, the query will act as if no filter criteria was passed to that column at all.

Consider the query below:

const user = await prisma.user.findFirst({
where: {
name: undefined,
},
})
Show query results

In this scenario, the query will return the very first record in the database.

Another way to represent the above query is:

const user = await prisma.user.findFirst()

Although this section's examples focused on the findFirst function, the same concepts apply to any function that affects a single record.

null and undefined in a GraphQL resolver

For this example, consider a database based on the following Prisma schema:

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}

In the following GraphQL mutation that updates a user, both authorEmail and name accept null. From a GraphQL perspective, this means that fields are optional:

type Mutation {
// Update author's email or name, or both - or neither!
updateUser(id: Int!, authorEmail: String, authorName: String): User!
}

However, if you pass null values for authorEmail or authorName on to Prisma Client, the following will happen:

  • If args.authorEmail is null, the query will fail. email does not accept null.
  • If args.authorName is null, Prisma Client changes the value of name to null. This is probably not how you want an update to work.
updateUser: (parent, args, ctx: Context) => {
return ctx.prisma.user.update({
where: { id: Number(args.id) },
data: {
| email: args.authorEmail, // email cannot be null
| name: args.authorName // name set to null - potentially unwanted behavior
},
})
},

Instead, set the value of email and name to undefined if the input value is null. Doing this is the same as not updating the field at all:

updateUser: (parent, args, ctx: Context) => {
return ctx.prisma.user.update({
where: { id: Number(args.id) },
data: {
| email: args.authorEmail != null ? args.authorEmail : undefined, // If null, do nothing
| name: args.authorName != null ? args.authorName : undefined // If null, do nothing
},
})
},

The effect of null and undefined on conditionals

There are some caveats to filtering with conditionals which might produce unexpected results. When filtering with conditionals you might expect one result but receive another given how Prisma Client treats nullable values.

The following table provides a high-level overview of how the different operators handle 0, 1 and n filters.

Operator0 filters1 filtern filters
ORreturn empty listvalidate single filtervalidate all filters
ANDreturn all itemsvalidate single filtervalidate all filters
NOTreturn all itemsvalidate single filtervalidate all filters

This example shows how an undefined parameter impacts the results returned by a query that uses the OR operator.

interface FormData {
name: string
email?: string
}

const formData: FormData = {
name: 'Emelie',
}

const users = await prisma.user.findMany({
where: {
OR: [
{
email: {
contains: formData.email,
},
},
],
},
})

// returns: []

The query receives filters from a formData object, which includes an optional email property. In this instance, the value of the email property is undefined. When this query is run no data is returned.

This is in contrast to the AND and NOT operators, which will both return all the users if you pass in an undefined value.

This is because passing an undefined value to an AND or NOT operator is the same as passing nothing at all, meaning the findMany query in the example will run without any filters and return all the users.

interface FormData {
name: string
email?: string
}

const formData: FormData = {
name: 'Emelie',
}

const users = await prisma.user.findMany({
where: {
AND: [
{
email: {
contains: formData.email,
},
},
],
},
})

// returns: { id: 1, email: '[email protected]', name: 'Emelie' }

const users = await prisma.user.findMany({
where: {
NOT: [
{
email: {
contains: formData.email,
},
},
],
},
})

// returns: { id: 1, email: '[email protected]', name: 'Emelie' }