RobertCraigie / prisma-client-py

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use
https://prisma-client-py.readthedocs.io
Apache License 2.0
1.85k stars 81 forks source link

v0.13.0 - ImportError: cannot import name 'AbstractEngine' from 'prisma.engine' #931

Closed jonluca closed 6 months ago

jonluca commented 6 months ago

Bug description

When we upgraded to version 0.13.0 we started seeing an exception in our import

Environment variables loaded from .env
Prisma schema loaded from schema.prisma
Error: Generator "prisma-client-py" failed:

Traceback (most recent call last):
  File "/home/azureuser/miniconda3/envs/replaypy11/bin/prisma-client-py", line 5, in <module>
    from prisma.cli import main
  File "/home/azureuser/miniconda3/envs/replaypy11/lib/python3.11/site-packages/prisma/__init__.py", line 27, in <module>
    from .client import *  # noqa: I001, TID251
    ^^^^^^^^^^^^^^^^^^^^^
  File "/home/azureuser/miniconda3/envs/replaypy11/lib/python3.11/site-packages/prisma/client.py", line 54, in <module>
    from .engine import AbstractEngine, QueryEngine, TransactionId
ImportError: cannot import name 'AbstractEngine' from 'prisma.engine' (/home/azureuser/miniconda3/envs/replaypy11/lib/python3.11/site-packages/prisma/engine/__init__.py)

The error appears to be in the generation of prisam.engine._types.

When you try and import from prisma.engine, it imports the instantiated versions of the query and abstract classes. CleanShot 2024-03-24 at 13 57 12@2x

However these say Error No module named 'prisma.engine._types'

CleanShot 2024-03-24 at 13 57 30@2x

How to reproduce

pip install prisma==0.13.0
npx -y prisma@5.11.0 generate

Expected behavior

The client generats properly

Environment & setup

jonluca commented 6 months ago

I think the issue lives in the jinja templates, because the error disappears if I update the type from ._types to .._types Before: CleanShot 2024-03-24 at 14 05 04@2x

After: CleanShot 2024-03-24 at 14 05 26@2x

And the same thing in query.py

But I cant seem to trace where in the code this template is coming from.

CleanShot 2024-03-24 at 14 07 36@2x

jonluca commented 6 months ago

ok i have no idea what happened but uninstall prisma and reinstalling it and then install prisma globally with node fixed it. I have no idea how the code could have codegened incorrectly but closing this for now

RobertCraigie commented 6 months ago

Ah sorry @jonluca this is an issue that happens when you've generated the client with one version, e.g. 0.12.0, and then upgrade to a newer version, e.g. 0.13.0. The old generated files are not removed when you install a new version for some reason, which then causes errors like this when internal code has been changed.

We provide a CLI for resetting the installation to a clean, non-generated state which would also solve your issue.

python -m prisma_cleanup

This has tripped a couple of people up so I'll see if I can provide a helpful error message in this case.

jonluca commented 6 months ago

Oh interesting that makes sense. That's what I thought it was so I did pip uninstall prisma but still saw it after that. This makes sense though, thanks!