Skip to content

Select fields

By default, when a query returns records (as opposed to a count), the result includes the default selection set:

  • All scalar fields defined in the Prisma schema (including enums)
  • None of the relation fields defined in the Prisma schema

To customize the result:

  • Use select to specify a subset of fields to include in the result.
  • Use include to specify a subset of relation fields to include in the result.

Example schema and provider

All examples are based on the following Prisma schema:

Expand to view the schema (schema.prisma)
prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "dart run orm"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id           Int      @id @default(autoincrement())
  email        String   @unique
  name         String?
  age          Int?
  role         Role     @default(USER)
  posts        Post[]
  Profile      Profile?
  country      String?
  city         String?
  profileViews Int      @default(0)
}

model Post {
  id         Int        @id @default(autoincrement())
  title      String
  content    String?
  published  Boolean    @default(false)
  author     User?      @relation(fields: [authorId], references: [id])
  authorId   Int?
  categories Category[]
  views      Int        @default(0)
  likes      Int        @default(0)
  tags       String[]   @default([])
}

model Profile {
  id     Int     @id @default(autoincrement())
  bio    String?
  user   User    @relation(fields: [userId], references: [id])
  userId Int     @unique
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

enum Role {
  USER
  ADMIN
}
Expand to view the provider (prisma.dart)
dart
import 'dart:async';

import 'prisma/generated_dart_client/client.dart';

/// Create a new instance of PrismaClient
final _client = PrismaClient();

/// Provide a PrismaClient instance to a function.
///
/// Wrapped in a function to ensure that the instance is diconnected
/// after the function is done.
FutureOr<T> providePrisma<T>(
    FutureOr<T> Function(PrismaClient prisma) main) async {
  try {
    return await main(_client);
  } finally {
    await _client.$disconnect();
  }
}

Return the default selection set

The following query returns the default selection set (all scalar fields, no relations):

dart
final user = await prisma.user.findUnique(
  where: UserWhereUniqueInput(id: 1),
);
Expand to view the result
json
{
  "id": 1,
  "name": "Seven",
  "email": "seven@odroe.com",
  "role": "ADMIN"
}

Select specific fields

Use select to return a limited subset of fields instead of all fields. The following example returns the email and name fields only:

dart
final user = await prisma.user.findUnique(
  where: UserWhereUniqueInput(email: "seven@odroe.com"),
  select: UserSelect(
    name: true,
    email: true,
  ),
);
Expand to view the result
json
{
  "name": "Seven",
  "email": "seven@odroe.com"
}

Include relations and select relation fields

Use include to return a subset of relation fields. The following example returns the name field of the User record, and the title field of the Post relation:

dart
final users = await prisma.user.findMany(
  select: UserSelect(
    name: true,
    posts: PrismaUnion.$2(
      UserPostsArgs(
        select: PostSelect(title: true),
      ),
    ),
  ),
);
Expand to view the result
json
{
  "name": "Seven",
  "posts": [
    {
      "title": "My first blog post"
    },
    {
      "title": "My second blog post"
    }
  ]
}

The following query uses select within an include, and returns all user fields and each post's title field:

dart
final users = await prisma.user.findMany(
  include: UserInclude(
    posts: PrismaUnion.$2(
      UserPostsArgs(
        select: PostSelect(title: true),
      ),
    ),
  ),
);
Expand to view the result
json
{
  "id": 1,
  "name": "Seven",
  "email": "seven@odroe.com",
  "role": "ADMIN",
  "posts": [
    {
      "title": "My first blog post"
    },
    {
      "title": "My second blog post"
    }
  ]
}

For more information about querying relations, refer to the following documentation:

Relation count

you can include or select a count of relations alongside fields - for example, a user's post count.

Released under the BSD-3-Clause License