SQLAlchemy Generator¶
BESSER provides a code generator that creates SQLAlchemy models to define the structure of a relational database. This structure (known as Declarative Mapping) defines the database metadata (using a Python object model) that will represent the input structural B-UML model.
Now, let’s generate the code for SQLAlchemy of our Structural model example.
For this, you should create the generator, provide the Structural model, and use
the generate method as follows:
from besser.generators.sql_alchemy import SQLAlchemyGenerator
generator: SQLAlchemyGenerator = SQLAlchemyGenerator(model=library_model)
generator.generate(dbms="sqlite")
The dbms parameter specifies the target database management system.
In this example, we use sqlite, but you can also specify postgresql, mysql, mssql, mariadb, or oracle.
Generated Output¶
The sql_alchemy.py file will be generated inside the <<current_directory>>/output
folder and it will look as follows.
1import enum
2from typing import List, Optional
3from sqlalchemy import (
4 create_engine, Column, ForeignKey, Table, Text, Boolean, String, Date,
5 Time, DateTime, Float, Integer, Enum
6)
7from sqlalchemy.orm import (
8 column_property, DeclarativeBase, Mapped, mapped_column, relationship
9)
10from datetime import datetime, time, date
11
12class Base(DeclarativeBase):
13 pass
14
15
16
17# Tables definition for many-to-many relationships
18book_author_assoc = Table(
19 "book_author_assoc",
20 Base.metadata,
21 Column("writtenBy", ForeignKey("author.id"), primary_key=True),
22 Column("publishes", ForeignKey("book.id"), primary_key=True),
23)
24
25# Tables definition
26class Author(Base):
27 __tablename__ = "author"
28 id: Mapped[int] = mapped_column(primary_key=True)
29 name: Mapped[str] = mapped_column(String(100))
30 email: Mapped[str] = mapped_column(String(100))
31
32class Book(Base):
33 __tablename__ = "book"
34 id: Mapped[int] = mapped_column(primary_key=True)
35 title: Mapped[str] = mapped_column(String(100))
36 pages: Mapped[int] = mapped_column(Integer)
37 release: Mapped[date] = mapped_column(Date)
38 locatedIn_id: Mapped[int] = mapped_column(ForeignKey("library.id"), nullable=False)
39
40class Library(Base):
41 __tablename__ = "library"
42 id: Mapped[int] = mapped_column(primary_key=True)
43 name: Mapped[str] = mapped_column(String(100))
44 address: Mapped[str] = mapped_column(String(100))
45
46
47#--- Relationships of the author table
48Author.publishes: Mapped[List["Book"]] = relationship("Book", secondary=book_author_assoc, back_populates="writtenBy")
49
50#--- Relationships of the book table
51Book.writtenBy: Mapped[List["Author"]] = relationship("Author", secondary=book_author_assoc, back_populates="publishes")
52Book.locatedIn: Mapped["Library"] = relationship("Library", back_populates="has", foreign_keys=[Book.locatedIn_id])
53
54#--- Relationships of the library table
55Library.has: Mapped[List["Book"]] = relationship("Book", back_populates="locatedIn", foreign_keys=[Book.locatedIn_id])
56
57# Database connection
58DATABASE_URL = "sqlite:///Library_model.db" # SQLite connection
59engine = create_engine(DATABASE_URL, echo=True)
60
61# Create tables in the database
62Base.metadata.create_all(engine, checkfirst=True)
Creating the Database¶
The generated code contains the SQLAlchemy classes that represent the database metadata. If you are using SQLite as DBMS, you can create the database and the tables by running the file as follows:
python sql_alchemy.py
This will create a SQLite database named Librarymodel.db in the same directory where the file is executed. The database
looks like this:
Note
Note that for DBMS different to SQLite, such as postgresql or mysql, you should change the connection string in the
generated code, providing the necessary information to connect to the database: username, password, host, port, and database name.
Warning
By default, inheritance is implemented using the Joined Table Inheritance. Other strategies like Concrete Table Inheritance is implemented only when you mark a parent class as abstract in the B-UML model, but there are limitations in Concrete inheritance, for example: the definition of relationships involving abstract classes are not supported. So only if the abstract class does not have relationships, it will be treated as a concrete parent class, otherwise, it will be treated as a parent class using Joined Table Inheritance.