question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow select as alias custom field name and exclude some fields in prisma client

See original GitHub issue

Problem

In prisma client 2.26 and sqlite, its not every efficient to run map, reduce server side functions to rename keys in data from prisma.

If prisma is getting upwards of five different relations, I would rather directly setup aliases, custom field names directly in the query than external code

similarly, if a table has a lot of field names, theres no clear way to exclude few while selecting all others.

I dont want to explicitly name every field in the prisma query if I need to select only 24 out of 25 fields in the model relation

Suggested solution

I am looking for the prisma equivalent of

SELECT party_id as id FROM genres 

and

SELECT * EXCEPT id
FROM (...) genres

and somehow apply it across multiple relations in findMany starting example

var tv_shows = await prisma.tVShow.findMany({
    include: { genres: true, production_companies: true, seasons: { include: { episodes: true } }, credits: true, similar: true }
  })

Alternatives

Doing an external code implementation using map, reduce to transform the selected object
ie

genres.map(item => {item.id = item.party_id; delete item.id;})

Additional context

related thread https://github.com/prisma/prisma/issues/4433

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:27
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

40reactions
paulintrognoncommented, Jan 13, 2022

I don’t know if it is the same issue, but I would love to be able to alias (rename) relations as well.

For example, what I would love:

const customer = await prisma.customer.findUnique({
  where { id },
  select: {
    fruits: {
      select: { id: true },
      where: { type: 'banana' },
      as: 'bananas'
    }
  }
})

// Bananas !
customer.bananas
15reactions
philippetedajocommented, Sep 13, 2022

Please, Add an alias feature to Prisma

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using custom model and field names (Concepts) - Prisma
You can "rename" fields and models that are used in the Prisma Client by mapping them to the "original" names in the database...
Read more >
Select fields (Concepts) - Prisma
This page explains how to select only a subset of a model's fields and/or include relations ("eager loading") in a Prisma Client query....
Read more >
Excluding fields - Prisma
This page explains how to exclude sensitive fields from Prisma Client.
Read more >
Data model (Reference) - Prisma
@map and @@map allow you to tune the shape of your Prisma Client API by decoupling model and field names from table and...
Read more >
Aggregation, grouping, and summarizing (Concepts) - Prisma
Prisma Client allows you to count records, aggregate number fields, and select distinct field values. Aggregate. Prisma Client allows you to aggregate on ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found