https://github.com/kmbhm1/supabase-pydantic
Generate Pydantic models other types from Supabase/Postgres schemas automatically. Ideal for FastAPI, Supabase, and rapid prototyping.
https://github.com/kmbhm1/supabase-pydantic
postgres pydantic-models pydantic-v2 python python3 python310 sqlalchemy supabase
Last synced: 12 days ago
JSON representation
Generate Pydantic models other types from Supabase/Postgres schemas automatically. Ideal for FastAPI, Supabase, and rapid prototyping.
- Host: GitHub
- URL: https://github.com/kmbhm1/supabase-pydantic
- Owner: kmbhm1
- License: mit
- Created: 2024-03-01T20:54:24.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-06T18:40:04.000Z (9 months ago)
- Last Synced: 2025-02-10T19:04:34.033Z (9 months ago)
- Topics: postgres, pydantic-models, pydantic-v2, python, python3, python310, sqlalchemy, supabase
- Language: Python
- Homepage: https://kmbhm1.github.io/supabase-pydantic
- Size: 1.19 MB
- Stars: 20
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: CODEOWNERS
- Security: SECURITY.md
- Support: SUPPORT.md
Awesome Lists containing this project
README
# Supabase Pydantic Schemas
[](https://pypi.org/project/supabase-pydantic/)
[
](https://anaconda.org/conda-forge/supabase-pydantic)
[](https://pydantic.dev)

[](https://codecov.io/github/kmbhm1/supabase-pydantic)


A project for generating Pydantic and SQLAlchemy models from Supabase and MySQL databases. This tool bridges the gap between your database schema and your Python code, providing type-safe models for FastAPI and other frameworks.
Currently, this is ideal for integrating [FastAPI](https://fastapi.tiangolo.com/) with [supabase-py](https://supabase.com/docs/reference/python/introduction) as a primary use-case, but more updates are coming! This project is inspired by the TS [type generating](https://supabase.com/docs/guides/api/rest/generating-types) capabilities of supabase cli. Its aim is to provide a similar experience for Python developers.
> **📣 NEW (Aug 2025)**: MySQL support! Generate models directly from MySQL databases with the `--db-type mysql` flag. [See the docs](https://kmbhm1.github.io/supabase-pydantic/examples/mysql-support/)
>
> **📣 NEW (Aug 2025)**: SQLAlchemy models with Insert and Update variants for better type safety. [Learn more](https://kmbhm1.github.io/supabase-pydantic/examples/sqlalchemy-models/)
## Installation
```bash
# Install with pip
$ pip install supabase-pydantic
# Or with conda
$ conda install -c conda-forge supabase-pydantic
```
## Configuration
Create a `.env` file with your database connection details:
```bash
DB_NAME=
DB_USER=
DB_PASS=
DB_HOST=
DB_PORT=
```
## Usage
### Generate Pydantic Models
Example with full command output (using a local connection):
```bash
$ sb-pydantic gen --type pydantic --framework fastapi --local
2025-08-18 15:47:42 - INFO - supabase_pydantic.db.connection:check_connection:72 - PostGres connection is open.
2025-08-18 15:47:42 - INFO - supabase_pydantic.db.connection:construct_tables:136 - Processing schema: public
2025-08-18 15:47:42 - INFO - supabase_pydantic.db.connection:__exit__:105 - PostGres connection is closed.
2025-08-18 15:47:42 - INFO - supabase_pydantic.cli.commands.gen:gen:239 - Generating Pydantic models...
2025-08-18 15:47:42 - INFO - supabase_pydantic.cli.commands.gen:gen:251 - Pydantic models generated successfully for schema 'public': /path/to/your/project/entities/fastapi/schema_public_latest.py
2025-08-18 15:47:42 - INFO - supabase_pydantic.cli.commands.gen:gen:258 - File formatted successfully: /path/to/your/project/entities/fastapi/schema_public_latest.py
```
### Common Commands
**Using a database URL:**
```bash
$ sb-pydantic gen --type pydantic --framework fastapi --db-url postgresql://postgres:postgres@127.0.0.1:54322/postgres
```
**Generating models for specific schemas:**
```bash
$ sb-pydantic gen --type pydantic --framework fastapi --local --schema extensions --schema auth
```
**Generate SQLAlchemy models:**
```bash
$ sb-pydantic gen --type sqlalchemy --local
```
**Using MySQL:**
```bash
$ sb-pydantic gen --type pydantic --framework fastapi --db-type mysql --db-url mysql://user:pass@localhost:3306/dbname
```
### Makefile Integration
```makefile
# Makefile examples for both Pydantic and SQLAlchemy generation
gen-pydantic:
@echo "Generating FastAPI Pydantic models..."
@sb-pydantic gen --type pydantic --framework fastapi --dir ./entities/fastapi --local
gen-sqlalchemy:
@echo "Generating SQLAlchemy ORM models..."
@sb-pydantic gen --type sqlalchemy --dir ./entities/sqlalchemy --local
# Generate all model types at once
gen-all: gen-pydantic gen-sqlalchemy
@echo "All models generated successfully."
```
For full documentation, visit [our docs site](https://kmbhm1.github.io/supabase-pydantic/).