# Including Relations and Selecting Fields — Prisma / TypeORM / Sequelize

Source: https://www.skillbyai.com/en/orms/or-loading-relations

> Load related records on purpose with include, relations or eager options, and fetch only the columns a screen actually needs.

## Ask for related data explicitly

None of the three ORMs loads relations unless you ask (TypeORM `eager` relations are the deliberate exception). **Prisma** uses `include: { posts: true }` to add relations, or `select` to pick exact fields and relations; you cannot use both at the same level of one query. **TypeORM** uses the `relations` option, such as `relations: { posts: true }`, which becomes a `LEFT JOIN`; a relation marked `eager: true` is joined automatically on every `find`, and a **lazy** relation typed as `Promise<Post[]>` runs a query the first time you `await` it. **Sequelize** uses `include: [{ model: Post, as: 'posts' }]` and by default builds a `JOIN`. Loading strategy matters. A join returns one wide row per child, repeating the parent columns, which can blow up when several one-to-many relations are joined together. Separate queries avoid that duplication but cost extra round trips. Pick consciously, and select only the columns you render: sending full rows with large text fields to a list page wastes bandwidth and memory.

## From parent query to joined result

The ORM first finds the parent rows, then loads relations by a join or a second query, then stitches everything into nested objects.

![Three connected stages: query the parent rows, load related rows, assemble nested result objects.](assets/figures/orms/section-5-map.svg) — Figure 5.1: parent query -> relation loading -> nested result

## Only what the list page needs

Each query returns authors with just the titles of their published posts. Note the shapes of the options.

```ts
// Prisma: select picks fields and nested relation fields
await prisma.user.findMany({
  select: {
    id: true,
    name: true,
    posts: { where: { published: true }, select: { title: true } },
  },
});

// TypeORM
await userRepo.find({
  select: { id: true, name: true, posts: { title: true } },
  relations: { posts: true },
  where: { posts: { published: true } },
});

// Sequelize
await User.findAll({
  attributes: ['id', 'name'],
  include: [{ model: Post, as: 'posts', attributes: ['title'], where: { published: true }, required: false }],
});
```

## A where inside include can drop parents

In Sequelize, adding `where` to an `include` makes it an inner join by default (`required: true`), so users with no matching posts vanish from the result. Set `required: false` to keep them. A similar effect happens in TypeORM when you filter on a relation in `where`: only parents with a matching child come back.

**Quiz:** Why can a Prisma query not use both include and select at the same level?

- [x] select already lets you choose relations and fields, so include at the same level would conflict
- [ ] include only works with MySQL
- [ ] select is deprecated
- [ ] include and select both run raw SQL

*Answer:* select already lets you choose relations and fields, so include at the same level would conflict. include means 'all scalar fields plus these relations'; select means 'exactly these fields and relations'. You can nest one inside the other, but not combine them at the same level.
