Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add Support for Selecting Specific Fields in Prisma Python #960

Open
afi-dev opened this issue Apr 28, 2024 · 1 comment

Comments

@afi-dev
Copy link

afi-dev commented Apr 28, 2024

Problem

Currently, when using Prisma Python, there is no direct support for selecting specific fields from a database query result.

Suggested solution

See: Prisma Docs : Select fields

I suggest adding support for the select feature in Prisma Python, similar to the existing functionality in other Prisma client libraries.

For example:

user = await prisma.user.find_first(
    where={
        "email": users.email,
    },
    select={
        "email": True,
        "name": True
    }
)

This would allow users to retrieve only the specified fields from the database query result, providing more flexibility and efficiency in data retrieval.

Alternatives

Without support for the select feature, users are forced to retrieve the entire record from the database and manually extract the required fields, which can be cumbersome and inefficient, especially for large datasets.

selected_fields = {"id": user.id, "username": user.username, "email": user.email,}
return selected_fields

Additional context

Adding support for the select feature in Prisma Python would align the functionality of the Python client with other Prisma client libraries, providing a consistent experience across different programming languages. This feature would greatly enhance the usability and versatility of Prisma Python for data retrieval tasks.

@umar-anzar
Copy link

umar-anzar commented May 22, 2024

prisma/schema.prisma

generator client {
  provider             = "prisma-client-py"
  interface            = "asyncio"
  recursive_type_depth = "5"
  partial_type_generator = "prisma/partial_types.py"
}

model User {
  id                  Int           @id @default(autoincrement())
  name                String        @db.VarChar(30)
 age Float
 password String
 country String
}

Run prisma generate

prisma/partial_types.py

from prisma.models import User
User.create_partial('UserWithName', include={'name'})

db.py or any where prisma client is intialize

import prisma
from prisma import Prisma

prisma_client: Prisma = Prisma()
prisma.register(prisma_client)

Run prisma generate

# Use of Partial Type
from prisma.partials import UserWithName

user = await UserWithName.prisma().find_first(
  where={
    'country': 'Scotland',
  },
)
print(user.name)

print(user.id)  # error `id` does not exist on the `UserWithName` type

Partial Types - Prisma Client Python (prisma-client-py.readthedocs.io)

Selecting Fields - Prisma Client Python (prisma-client-py.readthedocs.io)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants