SQLAlchemy Generator¶
- class besser.generators.sql_alchemy.sql_alchemy_generator.SQLAlchemyGenerator(model: DomainModel, output_dir: str = None)[source]¶
Bases:
GeneratorInterfaceSQLAlchemyGenerator is a class that implements the GeneratorInterface and is responsible for generating SQLAlchemy code based on B-UML models.
- Parameters:
model (DomainModel) – An instance of the DomainModel class representing the B-UML model.
output_dir (str, optional) – The output directory where the generated code will be saved. Defaults to None.
- RESERVED_NAMES = {'Base', 'Boolean_', 'Column_', 'DateTime_', 'Date_', 'Enum', 'Float_', 'ForeignKey_', 'Integer_', 'Interval_', 'List_', 'Mapped_', 'Optional_', 'PickleType_', 'String_', 'Table_', 'Text_', 'Time_', 'enum', 'os'}¶
- TYPES = {'any': 'PickleType_', 'bool': 'Boolean_', 'date': 'Date_', 'datetime': 'DateTime_', 'float': 'Float_', 'int': 'Integer_', 'str': 'String_(100)', 'time': 'Time_', 'timedelta': 'Interval_'}¶
- VALID_DBMS = {'mariadb', 'mssql', 'mysql', 'oracle', 'postgresql', 'sqlite'}¶
- _abc_impl = <_abc._abc_data object>¶
- generate(dbms: str = 'sqlite')[source]¶
Generates SQLAlchemy code based on the provided B-UML model and saves it to the specified output directory. If the output directory was not specified, the code generated will be stored in the <current directory>/output folder.
- Parameters:
dbms (str, optional) – The database management system to be used. Values allowed:
"sqlite"
"postgresql"
"mysql"
"mssql"
"mariadb"
"sqlite". (or "oracle". Defaults to)
- Returns:
None, but stores the generated code as a file named sql_alchemy.py
- get_concrete_table_inheritance()[source]¶
Determines if the model uses concrete table inheritance.
- Returns:
A list of class parents that use concrete table inheritance.
- Return type:
- get_ids()[source]¶
Returns a dictionary with the class names as keys and the id attributes as values.
- get_pk_py_types()[source]¶
Class name -> python type of its primary key (default ‘int’).
A ForeignKey column must use the SAME python type as the primary key it references — a
Mapped_[int]FK pointing at aStringPK breaks joins at runtime even though it imports. Delegates to the sharedstructural_utils.get_pk_py_typesso this generator and the backend’s path parameters can never disagree about a primary key’s type.
- separate_classes()[source]¶
Separates regular classes from association classes in the model.
- Returns:
A tuple containing two lists (regular_classes, association_classes)
- Return type:
- validate_attribute_types()[source]¶
Validates that every attribute type in the model maps to a known SQLAlchemy type.
Without this check, an unknown type would render as an empty string in the template and produce a generated file that crashes on import.
- Raises:
ValueError – If any attribute uses a type not present in TYPES.
- validate_model()[source]¶
Validates that the model doesn’t use reserved names for classes, enumerations, or attributes.
- Raises:
ValueError – If any reserved names are found in the model.