Skip to main content

Dinja

Dinja is a small tool for creating any text file format from a DBML database definition using Jinja2 templates. It can be used to auto-generate e.g. DDL, database access code or schema documentation from a single DBML file, maintaining a single source of truth for your database schema definition.

Its name is made from "DBML" and "Jinja2", but it's also name of Cucumis melo (a melon) 🍈 🍉 in Croatian.

Installation

Dinja is available on PyPI, so you can install it with pip:

pip install dinja-tool

Usage

1. Create your Jinja2 template(s)

The template files are of the destination file format but contain some special markers that Dinja replaces with the real database schema data. Their file names must end with either .jinja or .in. See the Jinja2 template documentation for how to write such a template.

The Python data structures provided to the templates are documented in the dinja.api module.

Dinja can render several template files at once - simply put them all into a single directory.

2. Run Dinja on your DBML file

The following command converts the dbschema.dbml file by rendering all template files inside the jinja_templates directory, storing the resulting files into the generated directory (creating one rendered file for each template).

$ dinja dbschema.dbml jinja_templates generated

The result files have the same names as their corresponding template files, but without the .jinja or .in suffix. Existing files are overwritten without a warning.

dinja -h or dinja --help prints a summary of all possible options and arguments.

Examples

Let's create and render a template for the example tables from the DBML introduction:

Table users {
  id integer
  username varchar
  role varchar
  created_at timestamp
}

Table posts {
  id integer [primary key]
  title varchar
  body text [note: 'Content of the post']
  user_id integer
  status integer
  created_at timestamp
}

Store this into the local file example.dbml.

For example, the following Jinja2 template generates SQLite2 DDL:

{% for table in tables %}
CREATE TABLE {{table.name}} (
    {% for column in table.columns %}
    "{{column.name}}" {{column.type.upper()}}
        {{-" PRIMARY KEY" if column.primary_key}}
        {{-" UNIQUE" if column.unique}}
        {{-" NOT NULL" if not column.nullable}}
        {{-" AUTOINCREMENT" if column.autoincrement}}
        {{-"," if not loop.last or table.primary_key or table.unique_constraints or table.references}}
    {% endfor %}
    {% if table.primary_key %}
    PRIMARY KEY({{table.primary_key|join(", ")}})
    {{-"," if table.unique_constraints or table.references}}
    {%endif %}
    {% for columns in table.unique_constraints %}
    UNIQUE({{columns|join(", ")}})
        {{- "," if not loop.last or table.references}}
    {% endfor %}
    {% for ref in table.references %}
    FOREIGN KEY("{{ref.local_columns|join(", ")}}") REFERENCES "{{ref.table_name}}" ("{{ref.remote_columns|join(", ")}}")
        {{-" ON DELETE {}".format(ref.delete_action) if ref.delete_action}}
        {{-"," if not loop.last}}
    {% endfor %}
);

{% endfor %}

Store it into a local file named schema_creation.sql.in within the templates directory, and run

$ dinja example.dbml templates .

This will create a new file schema_creation.sql with the following SQL content:

CREATE TABLE users (
    "id" INTEGER,
    "username" VARCHAR,
    "role" VARCHAR,
    "created_at" TIMESTAMP
);

CREATE TABLE posts (
    "id" INTEGER PRIMARY KEY,
    "title" VARCHAR,
    "body" TEXT,
    "user_id" INTEGER,
    "status" INTEGER,
    "created_at" TIMESTAMP
);

By creating Jinja2 templates according to your needs, you can generate literally any text-based file format from your DBML definition.

Copyright & License

Copyright 2025 by Thomas Wesenigk.

Licensed under the EUPL 1.2. Visit https://interoperable-europe.ec.europa.eu/collection/eupl/eupl-text-eupl-12 to read it in your favourite language.

Release files for dinja-tool 0.2.0

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

Source distribution (sdist)

Source distribution for dinja-tool 0.2.0
File Size Uploaded
dinja_tool-0.2.0.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dinja-tool 0.2.0
File Interpreter ABI Platform
dinja_tool-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.1 kB

Release files / dinja_tool-0.2.0.tar.gz

Download URL dinja_tool-0.2.0.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dfc62d0abbb89218c20a6192a7ff5c71d28e865d750562fea75f8ae849128412
BLAKE2b-256 checksum
How to use checksums
20f94f89cbab94e1e39651f0541aa641e2bfb62668afd324d9608842651b6aa1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.7 Linux/6.8.0-85-generic

Release files / dinja_tool-0.2.0-py3-none-any.whl

Download URL dinja_tool-0.2.0-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1698d4ae91c989ba07e79bd167b968c6a488930323722da0ec2c25bcedabad99
BLAKE2b-256 checksum
How to use checksums
d776a68dacca6644683ed40885f5e4b67726b915cf580373f0d7a0e1d92292c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.7 Linux/6.8.0-85-generic

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

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