Prisma to SQLAlchemy converter

Convert Prisma to SQLAlchemy 2.0 models for the same PostgreSQL database, for example to read the database from Python.

Prisma

SQLAlchemy

Waiting for input

What you get

How each result is checked

  1. Step 1: Your schema is validated and read by Prisma's own schema engine (Prisma 7), so it is understood exactly as Prisma would.
  2. Step 1: The SQL is parsed back with the real PostgreSQL parser and compared with what Prisma's engine says the schema means: every table and column, its PostgreSQL type (by Prisma's documented type mapping), NOT NULL, database defaults, primary keys, unique and normal indexes (names, columns, method, sort order), foreign keys (names, columns, ON DELETE/ON UPDATE), implicit many-to-many join tables and enums.
  3. Step 2: Your SQL is read by the real PostgreSQL parser, compiled to WebAssembly.
  4. Step 2: The models are loaded with the real SQLAlchemy 2.1 in your browser (CPython in WebAssembly); every mapper and relationship must configure.
  5. Step 2: SQLAlchemy is asked for the exact SQL it would run to create your database. That SQL is read with the PostgreSQL parser and compared with your input: every table, column type, NULL/NOT NULL, default, identity and generated column, primary key, unique constraint, foreign key (name, columns, ON DELETE/ON UPDATE), index (name, method, sort order, condition), CHECK constraint and enum.

What can't convert exactly

  • @default(uuid()), cuid(), nanoid(), ulid() and @updatedAt are filled in by Prisma Client, not the database, so those columns have no database default (same as prisma migrate). They are listed in the warnings.
  • With relationMode = "prisma" no foreign keys are created, as with prisma migrate.
  • Only the PostgreSQL provider is supported.
  • Tables without a primary key cannot be ORM classes; they are written as Table objects, as sqlacodegen does.
  • Types SQLAlchemy has no class for (point, int4range, ltree…) use a small SqlType class that keeps the exact database type.
  • Views, functions and triggers are not part of SQLAlchemy models and are listed as skipped.
  • The first run downloads Python and SQLAlchemy (about 6 MB compressed), once.

Questions

How is a two-step conversion checked?

Your input is converted to PostgreSQL (as prisma migrate writes it) first, then to the target format. Each step has its own independent check, and both must pass. The checks of both steps are listed under every result.

Is my data uploaded?

No. Everything runs in your browser, in a background worker. Nothing is sent to a server.

How do I use a Prisma database from Python?

Convert schema.prisma here to SQLAlchemy 2.0 models that describe the same tables, columns and constraints prisma migrate created. Your Python code can then read and write the database while the Node.js app keeps using Prisma.

How do I convert Prisma to SQLAlchemy online?

Paste your Prisma or open a file on the left (or click Example). The SQLAlchemy appears on the right as you type, already checked. Copy it, or download it as a file. Nothing is uploaded.

Can the SQLAlchemy output be wrong?

Every result is checked before it is shown (the checks are listed on this page). If a check fails, the page says "Review needed" and why, instead of presenting the output as correct, and anything that cannot convert exactly is named in the notes.