Skip to main content

Convert Prisma schema to SQLModel models

Project description

Prisma to SQLModel

A tool to convert Prisma schema files to SQLModel models. This tool helps you migrate from Prisma to SQLModel while preserving your data model structure, relationships, and indexes.

Features

  • Converts Prisma models to SQLModel classes
  • Preserves relationships (one-to-one, one-to-many, many-to-many)
  • Handles indexes and primary keys
  • Supports vector fields with pgvector
  • Maintains field types and constraints
  • Generates proper SQLModel relationships with back_populates

Installation

pip install prisma-to-sqlmodel

Usage

Command Line Interface

prisma-to-sqlmodel schema.prisma output.py

Python API

from prisma_to_sqlmodel import PrismaConverter

converter = PrismaConverter("schema.prisma")
python_code = converter.convert()

# Write to file
with open("output.py", "w") as f:
    f.write(python_code)

Example

Input (schema.prisma)

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  posts     Post[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String   @db.Text
  author    User     @relation(fields: [authorId], references: [id])
  authorId  Int
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Output (output.py)

from datetime import datetime
from typing import Optional, List
from sqlmodel import Field, SQLModel, Relationship

class User(SQLModel, table=True):
    __tablename__ = "User"

    id: int = Field(primary_key=True)
    email: str = Field(unique=True)
    name: Optional[str] = None
    posts: List["Post"] = Relationship(back_populates="author")
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(sa_column=Column(sql.func.now(), onupdate=sql.func.now()))

class Post(SQLModel, table=True):
    __tablename__ = "Post"

    id: int = Field(primary_key=True)
    title: str
    content: str = Field(sa_column=Column(Text))
    author: User = Relationship(back_populates="posts")
    author_id: int = Field(foreign_key="User.id")
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(sa_column=Column(sql.func.now(), onupdate=sql.func.now()))

Development

  1. Clone the repository
  2. Create a virtual environment: python -m venv .venv
  3. Activate the virtual environment:
    • Windows: .venv\Scripts\activate
    • Unix/MacOS: source .venv/bin/activate
  4. Install dependencies: pip install -e ".[dev]"
  5. Run tests: pytest

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prisma_to_sqlmodel-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prisma_to_sqlmodel-0.1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file prisma_to_sqlmodel-0.1.0.tar.gz.

File metadata

  • Download URL: prisma_to_sqlmodel-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for prisma_to_sqlmodel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6a268a8f7e76b94537d2d89fd98f90fb8f30f356b33c96ada948fb1d887c5d63
MD5 531dbab001a10a13ded0dd81af69e26a
BLAKE2b-256 0f8af27e70b4e3071ac46d4826bd8dac1ef97ad74c73545a34450999808a4721

See more details on using hashes here.

File details

Details for the file prisma_to_sqlmodel-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for prisma_to_sqlmodel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de2142215dba234bd7877b2d3bcf84671ae95f41bd1a300c494f7684edb82b17
MD5 7e0dfd4d4435352cbf10eab6d66574dc
BLAKE2b-256 b551327bb61681d5d3a1d89e4e44a67a77102a585f3342f36c22e864d394dfa7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page