SQLAlchemy Generator#

BESSER provides a code generator to produce the module-level constructors that will form the structure of a database managed with SQLAlchemy. This structure (known as Declarative Mapping) defines the database metadata (using a Python object model) that will represent the input B-UML model.

Now, let’s generate the code for SQLAlchemy of our Structural model example 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()

The sql_alchemy.py file with the Declarative Mapping of the database will be generated in the <<current_directory>>/output folder and it will look as follows.

 1from typing import List
 2from typing import Optional
 3from sqlalchemy import create_engine
 4from sqlalchemy import Column
 5from sqlalchemy import ForeignKey
 6from sqlalchemy import String
 7from sqlalchemy import Table
 8from sqlalchemy import Text
 9from sqlalchemy.orm import column_property
10from sqlalchemy.orm import DeclarativeBase
11from sqlalchemy.orm import Mapped
12from sqlalchemy.orm import mapped_column
13from sqlalchemy.orm import relationship
14from sqlalchemy import Boolean, String, Date, Time, DateTime, Float, Integer
15from datetime import datetime, time, date
16
17class Base(DeclarativeBase):
18    pass
19
20# Tables definition for many-to-many relationships
21book_author_assoc = Table(
22    "book_author_assoc",
23    Base.metadata,
24    Column("author_id", ForeignKey("author.id"), primary_key=True),
25    Column("book_id", ForeignKey("book.id"), primary_key=True),
26)
27
28# Tables definition
29class Book(Base):
30    
31    __tablename__ = "book"
32    id: Mapped[int] = mapped_column(primary_key=True)
33    pages: Mapped[int] = mapped_column(Integer)
34    release: Mapped[datetime] = mapped_column(DateTime)
35    title: Mapped[str] = mapped_column(String(100))
36
37class Author(Base):
38    
39    __tablename__ = "author"
40    id: Mapped[int] = mapped_column(primary_key=True)
41    name: Mapped[str] = mapped_column(String(100))
42    email: Mapped[str] = mapped_column(String(100))
43
44class Library(Base):
45    
46    __tablename__ = "library"
47    id: Mapped[int] = mapped_column(primary_key=True)
48    address: Mapped[str] = mapped_column(String(100))
49    name: Mapped[str] = mapped_column(String(100))
50
51
52#--- Foreign keys and relationships of the book table
53Book.writtenBy: Mapped[List["Author"]] = relationship("Author", secondary=book_author_assoc, back_populates="publishes")
54Book.library_id: Mapped["Library"] = mapped_column(ForeignKey("library.id"), nullable=False)
55Book.locatedIn: Mapped["Library"] = relationship("Library", back_populates="has")
56
57#--- Foreign keys and relationships of the author table
58Author.publishes: Mapped[List["Book"]] = relationship("Book", secondary=book_author_assoc, back_populates="writtenBy")
59
60#--- Foreign keys and relationships of the library table
61Library.has: Mapped[List["Book"]] = relationship("Book", back_populates="locatedIn")