Skip to main content

DBML parser for Python

Compliant with DBML v3.9.5 syntax

PyDBML is a Python parser and builder for DBML syntax.

The project was rewritten in May 2022, the new version 1.0.0 is not compatible with versions 0.x.x. See details in Upgrading to PyDBML 1.0.0.

Docs:

PyDBML requires Python v3.8 or higher

Installation

You can install PyDBML using pip:

pip3 install pydbml

Quick start

To parse a DBML file, import the PyDBML class and initialize it with Path object

>>> from pydbml import PyDBML
>>> from pathlib import Path
>>> parsed = PyDBML(Path('test_schema.dbml'))

or with file stream

>>> with open('test_schema.dbml') as f:
...     parsed = PyDBML(f)

or with entire source string

>>> with open('test_schema.dbml') as f:
...     source = f.read()
>>> parsed = PyDBML(source)
>>> parsed
<Database>

The parser returns a Database object that is a container for the parsed DBML entities.

You can access tables inside the tables attribute:

>>> for table in parsed.tables:
...     print(table.name)
...
orders
order_items
products
users
merchants
countries

Or just by getting items by index or full table name:

>>> parsed[1]
<Table 'public' 'order_items'>
>>> parsed['public.countries']
<Table 'public' 'countries'>

Other attributes are:

  • refs — list of all references,
  • enums — list of all enums,
  • table_groups — list of all table groups,
  • project — the Project object, if was defined.

Generate SQL for your DBML Database by accessing the sql property:

>>> print(parsed.sql)  # doctest:+ELLIPSIS
CREATE TYPE "orders_status" AS ENUM (
  'created',
  'running',
  'done',
  'failure'
);
<BLANKLINE>
CREATE TYPE "product status" AS ENUM (
  'Out of Stock',
  'In Stock'
);
<BLANKLINE>
CREATE TABLE "orders" (
  "id" int PRIMARY KEY AUTOINCREMENT,
  "user_id" int UNIQUE NOT NULL,
  "status" "orders_status",
  "created_at" varchar
);
...

Generate DBML for your Database by accessing the dbml property:

>>> parsed.project.items['author'] = 'John Doe'
>>> print(parsed.dbml)  # doctest:+ELLIPSIS
Project "test_schema" {
    author: 'John Doe'
    Note {
        'This schema is used for PyDBML doctest'
    }
}
<BLANKLINE>
Enum "orders_status" {
    "created"
    "running"
    "done"
    "failure"
}
<BLANKLINE>
Enum "product status" {
    "Out of Stock"
    "In Stock"
}
<BLANKLINE>
Table "orders" [headercolor: #fff] {
    "id" int [pk, increment]
    "user_id" int [unique, not null]
    "status" "orders_status"
    "created_at" varchar
}
<BLANKLINE>
Table "order_items" {
    "order_id" int
    "product_id" int
    "quantity" int [default: 1]
}
...

Release files for pydbml 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydbml 1.2.1
File Size Uploaded
pydbml-1.2.1.tar.gz 36.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydbml 1.2.1
File Interpreter ABI Platform
pydbml-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 82.8 kB

Release files / pydbml-1.2.1.tar.gz

Download URL pydbml-1.2.1.tar.gz
Size 36.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8c1232291948fd5857792aa732a33e71418c47f6a2badea869dc37377394566b
BLAKE2b-256 checksum
How to use checksums
1b7cd8baefcdd447d50fe24ab94e5344f9142e49f5d353e5f63863db0536dffd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / pydbml-1.2.1-py3-none-any.whl

Download URL pydbml-1.2.1-py3-none-any.whl
Size 46.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
709ad1d040268367a1be4653affd7dae206433f5e7a799ab5b32fad2ed8aa384
BLAKE2b-256 checksum
How to use checksums
433999396a0e876cef85155549b3a76144c77af8adc0f0c4f0e0dfc3d4ace109
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page