It's explained in the documentations of Select fields and Relation Queries That include and select have different usage:

By default, when a query returns records [..], the result includes the default selection set:

All scalar fields defined in the Prisma schema [..and] None of the relations

To change this behavior, you can use:

(1) Select:

allows you to either return a limited subset of fields instead of all fields:

const getUser: object | null = await prisma.user.findUnique({
  where: {
    id: 22,
  },
  select: {
    email: true,
    name: true,
  },
})

// Result
{
  name: "Alice",
  email: "[email protected]",
}

or include relations and select relation fields (nested usage):

const users = await prisma.user.findMany({
  select: {
    name: true,
    posts: {
      select: {
        title: true,
      },
    },
  },
})

(2) Include:

allows you to return some or all relation fields (which are not returned by default as mentioned before):

const getPosts = await prisma.post.findMany({
  where: {
    title: {
      contains: 'cookies',
    },
  },
  include: {
    author: true, // Return all fields
  },
})

// Result:
;[
  {
    id: 17,
    title: 'How to make cookies',
    published: true,
    authorId: 16,
    comments: null,
    views: 0,
    likes: 0,
    author: {
      id: 16,
      name: null,
      email: '[email protected]',
      profileViews: 0,
      role: 'USER',
      coinflips: [],
    },
  },
  {
    id: 21,
    title: 'How to make cookies',
    published: true,
    authorId: 19,
    comments: null,
    views: 0,
    likes: 0,
    author: {
      id: 19,
      name: null,
      email: '[email protected]',
      profileViews: 0,
      role: 'USER',
      coinflips: [],
    },
  },
]

(3) Select within Include:

Finally, you can use Select within Include, to return a subset of the relation fields.

const users = await prisma.user.findMany({
  // Returns all user fields
  include: {
    posts: {
      select: {
        title: true,
      },
    },
  },
})
Answer from mohamad albaz on Stack Overflow
🌐
Prisma
prisma.io › home › select fields › select fields › select fields › select fields
Select fields | Prisma Documentation
By default, Prisma Client returns all scalar fields for a model and no relations. Use select and include to make the result smaller, clearer, and more intentional.
🌐
Prisma
prisma.io › home › crud › crud › crud › crud
CRUD (Reference) | Prisma Documentation
June 3, 2022 - Configure Prisma Client with PgBouncer and other poolers: when to use pgbouncer=true, required transaction mode, prepared statements, and Prisma Migrate workarounds ... Learn how to return only the fields and relations you need with select and include in Prisma Client.
Discussions

node.js - NestJS Prisma ORM - Using 'select' versus 'include' when fetching data records? - Stack Overflow
Someone please explain the difference between using 'select' versus using 'include' when fetching data records (I'm a Prisma beginner - please keep it simple). Thanks in advance! ... It's explained in the documentations of Select fields and Relation Queries That include and select have different ... More on stackoverflow.com
🌐 stackoverflow.com
node.js - Select specific fields in Prisma not working - Stack Overflow
I would like to select specific fields return this.prisma.user.findFirst({ where: { password_hash: createHash('md5') .update(`${userId.id}test`) .digest('hex'), }, select: { More on stackoverflow.com
🌐 stackoverflow.com
Prisma client select all rows from a table
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) More on stackoverflow.com
🌐 stackoverflow.com
Way to select certain fields from related tables for prisma2
if I query a table together with a related table, how can I select only certain fields of that related table? More on github.com
🌐 github.com
12
September 30, 2019
🌐
GitHub
github.com › prisma › prisma › discussions › 20346
🔍 Setting Default Select Value for Prisma Fields · prisma/prisma · Discussion #20346
July 23, 2023 - In this example, only the id and name fields will be retrieved from the database, and createdAt, updatedAt, and deletedAt will be excluded. You can also write a Prisma Client extension for this as well. For example: const selectiveRetrievalExtension = { query: { user: { $allOperations({ args, query, operation }) { const { select } = args; // Extract the `select` field from query arguments // If `select` is not provided or doesn't include the special fields, // exclude the `createdAt`, `updatedAt`, and `deletedAt` fields from the query if (!select || (!select.createdAt && !select.updatedAt && !select.deletedAt)) { args.select = { ...select, id: true, name: true, createdAt: false, updatedAt: false, deletedAt: false, }; } return query(args); }, }, }, };
Author   prisma
🌐
Prisma
prisma.io › home › excluding fields › excluding fields › excluding fields › excluding fields
Excluding fields | Prisma Documentation
Use omit when you want Prisma Client ... where: { id: 1 }, omit: { password: true, }, }); ... Use select when you want to return only a small subset of fields....
🌐
Prisma Client Python
prisma-client-py.readthedocs.io › en › stable › reference › selecting-fields
Selecting Fields - Prisma Client Python - Read the Docs
As you can see you can still query against fields that aren't defined on the model itself but Prisma Client Python will only select the fields that are present.
🌐
Prisma
prisma.pub › queries › select-fields
Select fields | Prisma Dart
Use select to return a limited subset of fields instead of all fields. The following example returns the email and name fields only: ... final user = await prisma.user.findUnique( where: UserWhereUniqueInput(email: "seven@odroe.com"), select: UserSelect( name: true, email: true, ), );
🌐
Prisma
prisma.io › home › relation queries › relation queries › relation queries › relation queries
Relation queries (Concepts) | Prisma Documentation
Learn how to filter Prisma Client queries with where and sort results with orderBy. Nested readsRelation load strategies (Preview)ExamplesWhen to use which load strategy?Include a relationInclude all fields for a specific relationInclude deeply nested relationsSelect specific fields of included relationsRelation countFilter a list of relationsNested writesCreate a related recordCreate a single record and multiple related recordsUsing nested createUsing nested createManyCreate multiple records and multiple related recordsConnect multiple recordsConnect a single recordConnect or create a recordD
Find elsewhere
🌐
Prisma Client Python
prisma-client-py.readthedocs.io › en › v0.15.0 › reference › selecting-fields
Selecting Fields - Prisma Client Python
As you can see you can still query against fields that aren't defined on the model itself but Prisma Client Python will only select the fields that are present.
Top answer
1 of 1
25

It's explained in the documentations of Select fields and Relation Queries That include and select have different usage:

By default, when a query returns records [..], the result includes the default selection set:

All scalar fields defined in the Prisma schema [..and] None of the relations

To change this behavior, you can use:

(1) Select:

allows you to either return a limited subset of fields instead of all fields:

const getUser: object | null = await prisma.user.findUnique({
  where: {
    id: 22,
  },
  select: {
    email: true,
    name: true,
  },
})

// Result
{
  name: "Alice",
  email: "[email protected]",
}

or include relations and select relation fields (nested usage):

const users = await prisma.user.findMany({
  select: {
    name: true,
    posts: {
      select: {
        title: true,
      },
    },
  },
})

(2) Include:

allows you to return some or all relation fields (which are not returned by default as mentioned before):

const getPosts = await prisma.post.findMany({
  where: {
    title: {
      contains: 'cookies',
    },
  },
  include: {
    author: true, // Return all fields
  },
})

// Result:
;[
  {
    id: 17,
    title: 'How to make cookies',
    published: true,
    authorId: 16,
    comments: null,
    views: 0,
    likes: 0,
    author: {
      id: 16,
      name: null,
      email: '[email protected]',
      profileViews: 0,
      role: 'USER',
      coinflips: [],
    },
  },
  {
    id: 21,
    title: 'How to make cookies',
    published: true,
    authorId: 19,
    comments: null,
    views: 0,
    likes: 0,
    author: {
      id: 19,
      name: null,
      email: '[email protected]',
      profileViews: 0,
      role: 'USER',
      coinflips: [],
    },
  },
]

(3) Select within Include:

Finally, you can use Select within Include, to return a subset of the relation fields.

const users = await prisma.user.findMany({
  // Returns all user fields
  include: {
    posts: {
      select: {
        title: true,
      },
    },
  },
})
🌐
PalJS
paljs.com › plugins › select
GraphQL Enhancement Plugins | Packages | PalJS
Invalid fields may cause Prisma runtime errors. ... import dmmf from './generated/paljs/dmmf'; const select = new PrismaSelect(info, { dmmf: [dmmf], defaultFields: { User: { id: true } }, }).value;
🌐
GitHub
github.com › prisma › prisma › discussions › 11726
Why are all fields included in a SQL SELECT query? · prisma/prisma · Discussion #11726
Prisma only knows what your code tells Prisma. If you ask the GraphQL query for only a specific field, but then your code calls findMany for all fields, then Prisma will happily get and return all fields.
Author   prisma
🌐
Brendonovich
prisma.brendonovich.dev › reading-data › select-include
Select & Include - Prisma Client Rust - Brendonovich
Select also provides the ability to only fetch specific fields, whereas include will fetch all scalar fields and any specified relations.
🌐
GitHub
github.com › prisma › prisma-client-js › issues › 250
Way to select certain fields from related tables for prisma2 · Issue #250 · prisma/prisma-client-js
September 30, 2019 - Hello *, if I query a table together with a related table, how can I select only certain fields of that related table? const productsResult = await pt.products.findOne({ where: { product_id: id }, include:{ product_variants: true } }); I...
Author   prisma
🌐
GitHub
github.com › prisma › prisma › issues › 12894
Select everything except some fields · Issue #12894 · prisma/prisma
April 20, 2022 - I'm using prisma.user.findMany() to select users, but I don't want to select password or any other private field, instead of omitting the fields that I don't want, I have to select all the fields that I want. Ex. prisma.user.findMany({ s...
Author   prisma