PyDBML

1.2.1 · active · verified Thu Apr 16

PyDBML is a Python library for parsing and building DBML (Database Markup Language) files. It allows developers to convert DBML definitions into SQL DDL statements and vice-versa, facilitating database schema design and management. The current version is 1.2.1, and the project maintains an active release cadence with regular bug fixes and feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a DBML string using `pydbml.parse` and then render the parsed object to SQL DDL and back to DBML format.

from pydbml import parse

# Define a DBML string
dbml_content = """
Project "MyProject" {
    database_type: 'PostgreSQL'
    Note: 'A simple example project'
}

Table "users" {
    id int [pk, increment]
    username varchar [unique, not null]
    email varchar [unique]
    created_at timestamp [default: `now()`]
}

Table "posts" {
    id int [pk, increment]
    title varchar [not null]
    content text
    user_id int [ref: > users.id]
    created_at timestamp [default: `now()`]
}
"""

# Parse the DBML content
try:
    parsed_dbml = parse(dbml_content)
    print("DBML parsed successfully.")

    # Render to SQL DDL
    sql_output = parsed_dbml.sql()
    print("\n--- Generated SQL ---")
    print(sql_output)

    # Optionally, render back to DBML (can differ slightly in formatting)
    dbml_output = parsed_dbml.dbml()
    print("\n--- Generated DBML (re-rendered) ---")
    print(dbml_output)

except Exception as e:
    print(f"An error occurred during parsing or rendering: {e}")

view raw JSON →