Prisma Client Py Versions Save

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use

v0.4.3

2 years ago

Bug fixes

  • Correctly render Enum fields within compound keys (#190)

What's Changed

Subclassing pseudo-recursive models

Subclassing pseudo-recursive models will now raise a warning instead of crashing, static types will still not respect the subclass, for example:

from prisma.models import User

class MyUser(User):
    @property
    def fullname(self) -> str:
        return f'{self.name} {self.surname}'

# static type checkers will think that `user` is an instance of `User` when it is actually `MyUser` at runtime
# you can fix this by following the steps here:
# https://prisma-client-py.readthedocs.io/en/stable/reference/limitations/#removing-limitations
user = MyUser.prisma().create(
    data={
        'name': 'Robert',
        'surname': 'Craigie',
    },
)

For more details, see the documentation

Default HTTP timeout increased

The default HTTP timeout used to communicate with the internal Query Engine has been increased from 5 seconds to 30 seconds, this means you should no longer encounter timeout errors when executing very large queries.

Customise the internal HTTPX Client

You can now customise the HTTPX Client used to communicate with the internal query engine, this could be useful if you need to increase the http timeout, for full reference see the documentation.

client = Client(
    http={
        'timeout': 100,
    },
)

Prisma Upgrade

The internal Prisma binaries that Prisma Client Python makes use of have been upgraded from v3.4.0 to v3.7.0 for a full changelog see:

Create partial models without any relational fields

Instead of having to manually update the list of excluded fields when creating partial models whenever a new relation is added you can now just use exclude_relational_fields=True!

from prisma.models import User
User.create_partial('UserWithoutRelations', exclude_relational_fields=True)
class UserWithoutRelations:
  id: str
  name: str
  email: Optional[str]

v0.4.2

2 years ago

What's changed

This release is a patch release, fixing a bug introduced in the dev CLI in v0.4.1 (#182)

v0.4.1

2 years ago

What's Changed

Custom Prisma Python Generators

You can now easily write your own Prisma generators in Python!

For example:

generator.py

from pathlib import Path
from prisma.generator import BaseGenerator, Manifest, models


class MyGenerator(BaseGenerator):
    def get_manifest(self) -> Manifest:
        return Manifest(
            name='My Prisma Generator',
            default_output=Path(__file__).parent / 'generated.md',
        )

    def generate(self, data: Data) -> None:
        lines = [
            '# My Prisma Models!\n',
        ]
        for model in data.dmmf.datamodel.models:
            lines.append(f'- {model.name}')

        output = Path(data.generator.output.value)
        output.write_text('\n'.join(lines))

if __name__ == '__main__':
    MyGenerator.invoke()

Then you can add the generator to your Prisma Schema file like so:

generator custom {
  provider = "python generator.py"
}

Your custom generator will then be invoked whenever you run prisma generate

$ prisma generate
Prisma schema loaded from tests/data/schema.prisma

✔ Generated My Prisma Generator to ./generated.md in 497ms

For more details see the documentation: https://prisma-client-py.readthedocs.io/en/latest/reference/custom-generators/

Connect with a Context Manager

You can now use the Client as a context manager to automatically connect and disconnect from the database, for example:

from prisma import Client

async with Client() as client:
    await client.user.create(
        data={
            'name': 'Robert',
        },
    )

For more information see the documentation: https://prisma-client-py.readthedocs.io/en/stable/reference/client/#context-manager

Automatically register the Client instance

You can now automatically register the Client when it is created:

from prisma import Client

client = Client(auto_register=True)

Which is equivalent to:

from prisma import Client, register

client = Client()
register(client)

Support for setting a default connection timeout

The default timeout used for connecting to the database can now be set at the client level, for example:

from prisma import Client

client = Client(connect_timeout=5)

You can still explicitly specify the timeout when connecting, for example:

from prisma import Client

client = Client(connect_timeout=5)
client.connect()  # timeout: 5
client.connect(timeout=10)  # timeout: 10

Bug Fixes

  • Fixed unintentional blocking IO when decoding HTTP responses when using async (#169)
  • DateTime microsecond precision is now truncated to 3 places (#129)

v0.4.0

2 years ago

Breaking Changes

The following field names are now restricted and attempting to generate the client with any of them will now raise an error:

  • startswith
  • endswith
  • order_by
  • not_in
  • is_not

What's Changed

Support for the Bytes type

You can now create models that make use of binary data, this is stored in the underlying database as Base64 data, for example:

model User {
  id     Int @id @default(autoincrement())
  name   String
  binary Bytes
}
from prisma import Base64
from prisma.models import User

user = await User.prisma().create(
    data={
        'name': 'Robert',
        'binary': Base64.encode(b'my binary data'),
    },
)
print(f'binary data: {user.binary.decode()}')

Support for scalar list fields

You can now query for and update scalar list fields, for example:

model User {
  id     Int @id @default(autoincrement())
  emails String[]
}
user = await client.user.find_first(
    where={
        'emails': {
            'has': '[email protected]',
        },
    },
)

For more details, visit the documentation: https://prisma-client-py.readthedocs.io/en/latest/reference/operations/#lists-fields

Argument deprecations

The order argument to the count() method has been deprecated, this will be removed in the next release.

Action Docstrings

All query action methods now have auto-generated docstrings specific for each model, this means that additional documentation will be shown when you hover over the method call in your IDE, for example:

Image showcasing docstring on hover

Other Changes

  • typing-extensions is now a required dependency

v0.3.0

2 years ago

Breaking Changes

The prisma field name is now reserved, trying to generate a model that has a field called prisma will raise an error.

You can, however, still create a model that uses the prisma field name at the database level.

model User {
  id           String @id @default(cuid())
  prisma_field String @map("prisma")
}

What's Changed

Querying from Model Classes

You can now run prisma queries directly from model classes, for example:

from prisma.models import User

user = await User.prisma().create(
    data={
        'name': 'Robert',
    },
)

This API is exactly the same as the previous client-based API.

To get starting running queries from model classes, you must first register the prisma client instance that will be used to communicate with the database.

from prisma import Client, register

client = Client()
register(client)
await client.connect()

For more details, visit the documentation.

Count non-null fields

You can now select which fields are returned by count().

This returns a dictionary matching the fields that are passed in the select argument.

from prisma.models import Post

results = await Post.prisma().count(
    select={
        '_all': True,
        'description': True,
    },
)
# {'_all': 3, 'description': 2}

Support for Python 3.10

Python 3.10 is now officially supported.

Prisma Update

The internal Prisma binaries that Prisma Client Python uses have been upgraded from 3.3.0 to 3.4.0.

  • Support for PostgreSQL 14
  • prisma db push support for MongoDB

Improved Client Generation Message

The current version of the client will now be displayed post-generation:

Prisma schema loaded from schema.prisma

✔ Generated Prisma Client Python (v0.3.0) to ./.venv/lib/python3.9/site-packages/prisma in 765ms

Improved Generation Error Message

An explicit and helpful message is now shown when attempting to generate the Python Client using an unexpected version of Prisma.

Environment variables loaded from .env
Prisma schema loaded from tests/data/schema.prisma
Error: 
  Prisma Client Python expected Prisma version: 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 but got: da6fafb57b24e0b61ca20960c64e2d41f9e8cff1
  If this is intentional, set the PRISMA_PY_DEBUG_GENERATOR environment variable to 1 and try again.
  Are you sure you are generating the client using the python CLI?
  e.g. python3 -m prisma generate (type=value_error)

Other Changes

  • added --type-depth option to prisma py generate

v0.2.4

2 years ago

This release is a patch release, the v0.2.3 release erroneously contained auto-generated files.

v0.2.3

2 years ago

🚨 DO NOT INSTALL FROM THIS VERSION 🚨

This release has been yanked from PyPi as it contained auto-generated files, please install using 0.2.4 or greater.

Bug Fixes

  • Partial types with enum fields are now correctly generated (#84)

Prisma Update

The internal Prisma binaries that Prisma Client Python uses have been upgraded from 3.1.1 to 3.3.0.

  • MongoDB introspection support is in preview

For a full list of changes see https://github.com/prisma/prisma/releases/tag/3.2.0 and https://github.com/prisma/prisma/releases/tag/3.3.0

v0.2.2

2 years ago

Package Rename

The python package has been renamed from prisma-client to prisma!

You can now install the client like so:

pip install prisma

You can still install using the old package name, however no new releases will be published.

Datasource Overriding

The datasource can be dynamically overriden when the client is instantiated:

from prisma import Client

client = Client(
    datasource={
        'url': 'file:./dev_qa.db',
    },
)

This is especially useful for testing purposes.

v0.2.1

2 years ago

New features

Support for case insensitive string filtering

This feature is only supported when using PostgreSQL and MongoDB.

user = await client.user.find_first(
    where={
        'name': {
            'contains': 'robert',
            'mode': 'insensitive',
        },
    },
)

Prisma Update

The internal Prisma binaries that Prisma Client Python uses have been upgraded from 2.30.0 to 3.1.1.

This brings with it a lot of new features and improvements:

  • Referential Actions
  • Named Constraints
  • Microsoft SQL Server and Azure SQL Connector

For a full list of changes see https://github.com/prisma/prisma/releases/tag/3.1.1 and https://github.com/prisma/prisma/releases/tag/3.0.1

Type Validator

Prisma Client Python now comes bundled with a type validator, this makes it much easier to pass untrusted / untyped arguments to queries in a robust and type safe manner:

import prisma
from prisma.types import UserCreateInput

def get_untrusted_input():
    return {'points': input('Enter how many points you have: ')}

data = prisma.validate(UserCreateInput, get_untrusted_input())
await client.user.create(data=data)

Any invalid input would then raise an easy to understand error (note: edited for brevity):

Enter how many points you have: a lot
Traceback:
pydantic.error_wrappers.ValidationError: 1 validation error for UserCreateInput
points
  value is not a valid integer (type=type_error.integer)

Minor Changes

  • Improved supported for the windows platform (not officially supported yet)

v0.2.0

2 years ago

🚨 This release contains breaking changes 🚨

Filtering field types by NOT and IN has been renamed to not and in.

For example:

post = await client.post.find_first(
    where={
        'title': {
            'NOT': 'Exclude title',
        },
    },
)

Must now be written as:

post = await client.post.find_first(
    where={
        'title': {
            'not': 'Exclude title',
        },
    },
)

Bug fixes

  • Fixes filtering records by NOT (#70)