Skip to content

sqlalchemy

adjust_through_many_to_many_model(model_field)

Registers m2m relation on through model. Sets ormar.ForeignKey from through model to both child and parent models, unless the through model already declares them (see resolve_through_fields). Sets sqlalchemy.ForeignKey to both child and parent models. Sets pydantic fields with child and parent model types.

Parameters:

Name Type Description Default
model_field ManyToManyField

relation field defined in parent model

required
Source code in ormar/models/helpers/sqlalchemy.py
Python
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def adjust_through_many_to_many_model(model_field: "ManyToManyField") -> None:
    """
    Registers m2m relation on through model.
    Sets ormar.ForeignKey from through model to both child and parent models,
    unless the through model already declares them (see resolve_through_fields).
    Sets sqlalchemy.ForeignKey to both child and parent models.
    Sets pydantic fields with child and parent model types.

    :param model_field: relation field defined in parent model
    :type model_field: ManyToManyField
    """
    model_fields = model_field.through.ormar_config.model_fields
    sides = (
        (
            model_field.default_target_field_name(),
            model_field.to,
            model_field.through_reverse_foreign_key_name,
            model_field.through_reverse_relation_nullable,
        ),
        (
            model_field.default_source_field_name(),
            model_field.owner,
            model_field.through_foreign_key_name,
            model_field.through_relation_nullable,
        ),
    )
    for field_name, target, foreign_key_name, nullable in sides:
        existing = model_fields.get(field_name)
        if (
            existing is not None
            and existing.is_relation
            and not existing.virtual
            and existing.to is target
        ):
            continue
        model_fields[field_name] = ormar.ForeignKey(  # type: ignore
            to=target,
            real_name=field_name,
            ondelete=None if target.ormar_config.view else "CASCADE",
            db_constraint=not target.ormar_config.view,
            owner=model_field.through,
        )
        create_and_append_m2m_fk(
            model=target,
            model_field=model_field,
            field_name=field_name,
            foreign_key_name=foreign_key_name,
            nullable=nullable,
        )
        create_pydantic_field(
            field_name=field_name, model=target, model_field=model_field
        )
        setattr(model_field.through, field_name, RelationDescriptor(name=field_name))

append_multi_column_m2m_fk(through_field, foreign_key_name, nullable)

Add the columns of a through model foreign key to a model with a composite primary key (<field>_<key column>), with one composite FOREIGN KEY constraint (ON DELETE / ON UPDATE CASCADE), built by the foreign key field itself.

Parameters:

Name Type Description Default
through_field ForeignKeyField

foreign key of the through model to that model

required
foreign_key_name Optional[str]

name of the constraint, default generated

required
nullable bool

whether the columns are nullable

required
Source code in ormar/models/helpers/sqlalchemy.py
Python
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def append_multi_column_m2m_fk(
    through_field: "ForeignKeyField",
    foreign_key_name: Optional[str],
    nullable: bool,
) -> None:
    """
    Add the columns of a through model foreign key to a model with a composite
    primary key (``<field>_<key column>``), with one composite
    ``FOREIGN KEY`` constraint (``ON DELETE / ON UPDATE CASCADE``), built by
    the foreign key field itself.

    :param through_field: foreign key of the through model to that model
    :type through_field: ForeignKeyField
    :param foreign_key_name: name of the constraint, default generated
    :type foreign_key_name: Optional[str]
    :param nullable: whether the columns are nullable
    :type nullable: bool
    """
    through = through_field.owner
    table = through.ormar_config.table
    through_field.sql_nullable = nullable
    through_field.onupdate = "CASCADE"
    if foreign_key_name is not None:
        through_field.foreign_key_name = foreign_key_name
    for column in through_field.get_columns():
        through.ormar_config.columns.append(column)
        table.append_column(column, replace_existing=True)
    for constraint in through_field.construct_table_constraints():
        table.append_constraint(constraint)

check_for_null_type_columns_from_forward_refs(config)

Check is any column is of NUllType() meaning it's empty column from ForwardRef

Parameters:

Name Type Description Default
config OrmarConfig

OrmarConfig of the Model without sqlalchemy table constructed

required

Returns:

Type Description
bool

result of the check

Source code in ormar/models/helpers/sqlalchemy.py
Python
524
525
526
527
528
529
530
531
532
533
534
535
def check_for_null_type_columns_from_forward_refs(config: "OrmarConfig") -> bool:
    """
    Check is any column is of NUllType() meaning it's empty column from ForwardRef

    :param config: OrmarConfig of the Model without sqlalchemy table constructed
    :type config: Model class OrmarConfig
    :return: result of the check
    :rtype: bool
    """
    return not any(
        isinstance(col.type, sqlalchemy.sql.sqltypes.NullType) for col in config.columns
    )

column_owners(model)

Parameters:

Name Type Description Default
model Union[type[Model], type[NewBaseModel]]

model whose fields are inspected

required

Returns:

Type Description
dict[str, list[BaseField]]

fields storing each database column, in declaration order

Source code in ormar/models/helpers/sqlalchemy.py
Python
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def column_owners(
    model: Union[type["Model"], type["NewBaseModel"]],
) -> dict[str, list["BaseField"]]:
    """
    :param model: model whose fields are inspected
    :type model: Union[type[Model], type[NewBaseModel]]
    :return: fields storing each database column, in declaration order
    :rtype: dict[str, list[BaseField]]
    """
    owners: dict[str, list["BaseField"]] = {}
    for field in model.ormar_config.model_fields.values():
        if _is_db_field(field):
            for column in field.column_aliases():
                owners.setdefault(column, []).append(field)
    return owners

create_and_append_m2m_fk(model, model_field, field_name, foreign_key_name=None, nullable=True)

Registers sqlalchemy Column with sqlalchemy.ForeignKey leading to the model.

Newly created field is added to m2m relation through model OrmarConfig columns and table.

Parameters:

Name Type Description Default
field_name str

name of the column to create

required
model type[Model]

Model class to which FK should be created

required
model_field ManyToManyField

field with ManyToMany relation

required
foreign_key_name Optional[str]

optional override for the generated FK constraint name.

None
nullable bool

whether the created column is nullable.

True
Source code in ormar/models/helpers/sqlalchemy.py
Python
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def create_and_append_m2m_fk(
    model: type["Model"],
    model_field: "ManyToManyField",
    field_name: str,
    foreign_key_name: Optional[str] = None,
    nullable: bool = True,
) -> None:
    """
    Registers sqlalchemy Column with sqlalchemy.ForeignKey leading to the model.

    Newly created field is added to m2m relation
    through model OrmarConfig columns and table.

    :param field_name: name of the column to create
    :type field_name: str
    :param model: Model class to which FK should be created
    :type model: Model class
    :param model_field: field with ManyToMany relation
    :type model_field: ManyToManyField field
    :param foreign_key_name: optional override for the generated FK constraint name.
    :type foreign_key_name: Optional[str]
    :param nullable: whether the created column is nullable.
    :type nullable: bool
    """
    through_field = cast(
        "ForeignKeyField", model_field.through.ormar_config.model_fields[field_name]
    )
    if len(through_field.link.pairs) > 1:
        append_multi_column_m2m_fk(
            through_field=through_field,
            foreign_key_name=foreign_key_name,
            nullable=nullable,
        )
        return
    (pk_alias,) = model.ormar_config.pk.column_aliases()
    pk_column = next(
        (col for col in model.ormar_config.columns if col.name == pk_alias), None
    )
    if pk_column is None:  # pragma: no cover
        raise ormar.ModelDefinitionError(
            "ManyToMany relation cannot lead to field without pk"
        )
    through_table = model_field.through.ormar_config.tablename
    target_table = model.ormar_config.tablename
    default_name = f"fk_{through_table}_{target_table}_{field_name}_{pk_alias}"
    foreign_keys = (
        []
        if model.ormar_config.view
        else [
            sqlalchemy.schema.ForeignKey(
                qualified_fk_reference(target=model, column_alias=pk_alias),
                ondelete="CASCADE",
                onupdate="CASCADE",
                name=foreign_key_name or default_name,
            )
        ]
    )
    column = sqlalchemy.Column(
        field_name,
        pk_column.type,
        *foreign_keys,
        nullable=nullable,
    )
    model_field.through.ormar_config.columns.append(column)
    model_field.through.ormar_config.table.append_column(column, replace_existing=True)

index_name_fragment(expression)

Returns an identifier-safe name fragment for an index column or SQL expression, so auto-generated index names work for functional indexes (e.g. LOWER(name)) as well as plain columns.

Parameters:

Name Type Description Default
expression Any

an index column name or SQL expression element

required

Returns:

Type Description
str

sanitized fragment containing only alphanumerics and underscores

Source code in ormar/models/helpers/sqlalchemy.py
Python
574
575
576
577
578
579
580
581
582
583
584
585
def index_name_fragment(expression: Any) -> str:
    """
    Returns an identifier-safe name fragment for an index column or SQL
    expression, so auto-generated index names work for functional indexes
    (e.g. LOWER(name)) as well as plain columns.

    :param expression: an index column name or SQL expression element
    :type expression: Any
    :return: sanitized fragment containing only alphanumerics and underscores
    :rtype: str
    """
    return re.sub(r"[^0-9a-zA-Z]+", "_", str(expression)).strip("_")

merge_shared_columns(model, columns)

Keep one column per name when several fields store the same column (e.g. a tenant_id shared by two foreign keys of a link table), built by merged_column.

Parameters:

Name Type Description Default
model Union[type[Model], type[NewBaseModel]]

model the columns belong to

required
columns list[Column]

columns of the model's fields, in declaration order

required

Returns:

Type Description
list[sqlalchemy.Column]

columns with each name once

Raises:

Type Description
ModelDefinitionError

if the fields can't share a column

Source code in ormar/models/helpers/sqlalchemy.py
Python
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
def merge_shared_columns(
    model: Union[type["Model"], type["NewBaseModel"]],
    columns: list[sqlalchemy.Column],
) -> list[sqlalchemy.Column]:
    """
    Keep one column per name when several fields store the same column (e.g.
    a ``tenant_id`` shared by two foreign keys of a link table), built by
    ``merged_column``.

    :raises ModelDefinitionError: if the fields can't share a column
    :param model: model the columns belong to
    :type model: Union[type[Model], type[NewBaseModel]]
    :param columns: columns of the model's fields, in declaration order
    :type columns: list[sqlalchemy.Column]
    :return: columns with each name once
    :rtype: list[sqlalchemy.Column]
    """
    by_name: dict[str, list[sqlalchemy.Column]] = {}
    for column in columns:
        by_name.setdefault(column.name, []).append(column)
    if all(len(same) == 1 for same in by_name.values()):
        return columns
    owners = column_owners(model=model)
    merged = []
    for name, same in by_name.items():
        if len(same) == 1:
            merged.append(same[0])
            continue
        validate_shared_column(
            model=model, name=name, fields=owners[name], columns=same
        )
        merged.append(merged_column(name=name, fields=owners[name], columns=same))
    return merged

merged_column(name, fields, columns)

Build the one column several fields share (all of them give it the same type). It is part of the primary key when any of the fields is, nullable only when all of them are and never generated by the database. Fields storing the column on their own (a plain field, foreign keys of one column) add their foreign keys, index and unique flags; the default, server default and comment come from the plain field (foreign keys set none).

Parameters:

Name Type Description Default
name str

column name

required
fields list[BaseField]

fields storing the column, in declaration order

required
columns list[Column]

the column as each field defines it

required

Returns:

Type Description
sqlalchemy.Column

the shared column

Source code in ormar/models/helpers/sqlalchemy.py
Python
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
def merged_column(
    name: str,
    fields: list["BaseField"],
    columns: list[sqlalchemy.Column],
) -> sqlalchemy.Column:
    """
    Build the one column several fields share (all of them give it the same
    type). It is part of the primary key when any of the fields is, nullable
    only when all of them are and never generated by the database. Fields
    storing the column on their own (a plain field, foreign keys of one
    column) add their foreign keys, index and unique flags; the default,
    server default and comment come from the plain field (foreign keys set
    none).

    :param name: column name
    :type name: str
    :param fields: fields storing the column, in declaration order
    :type fields: list[BaseField]
    :param columns: the column as each field defines it
    :type columns: list[sqlalchemy.Column]
    :return: the shared column
    :rtype: sqlalchemy.Column
    """
    own = [field for field in fields if len(field.column_aliases()) == 1]
    plain = next((field for field in own if not field.is_relation), None)
    foreign_keys = [
        foreign_key for field in own for foreign_key in field.construct_constraints()
    ]
    return sqlalchemy.Column(
        name,
        columns[0].type,
        *foreign_keys,
        primary_key=any(column.primary_key for column in columns),
        autoincrement=False,
        nullable=all(column.nullable for column in columns),
        index=any(field.index for field in own) or None,
        unique=any(field.unique for field in own) or None,
        default=plain.ormar_default if plain is not None else None,
        server_default=plain.server_default if plain is not None else None,
        comment=plain.comment if plain is not None else None,
    )

names_columns_explicitly(field)

Parameters:

Name Type Description Default
field BaseField

field storing a column

required

Returns:

Type Description
bool

whether the field is a foreign key naming its columns itself (columns= / name=), so a column it shares with another field is meant, not a clash of generated names

Source code in ormar/models/helpers/sqlalchemy.py
Python
273
274
275
276
277
278
279
280
281
282
283
284
def names_columns_explicitly(field: "BaseField") -> bool:
    """
    :param field: field storing a column
    :type field: BaseField
    :return: whether the field is a foreign key naming its columns itself
        (``columns=`` / ``name=``), so a column it shares with another field
        is meant, not a clash of generated names
    :rtype: bool
    """
    return field.is_relation and (
        cast("ForeignKeyField", field).columns is not None or bool(field.db_alias)
    )

populate_config_sqlalchemy_table_if_required(config)

Constructs sqlalchemy table out of columns and parameters set on OrmarConfig. It populates name, metadata, columns and constraints.

Parameters:

Name Type Description Default
config OrmarConfig

OrmarConfig of the Model without sqlalchemy table constructed

required
Source code in ormar/models/helpers/sqlalchemy.py
Python
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
def populate_config_sqlalchemy_table_if_required(config: "OrmarConfig") -> None:
    """
    Constructs sqlalchemy table out of columns and parameters set on OrmarConfig.
    It populates name, metadata, columns and constraints.

    :param config: OrmarConfig of the Model without sqlalchemy table constructed
    :type config: Model class OrmarConfig
    """
    if config.table is None and check_for_null_type_columns_from_forward_refs(
        config=config
    ):
        set_constraint_names(config=config)
        # a view gets its own metadata so create_all and Alembic leave it alone
        table = sqlalchemy.Table(
            config.tablename,
            (
                sqlalchemy.MetaData(
                    schema=config.metadata.schema,
                    naming_convention=config.metadata.naming_convention,
                )
                if config.view
                else config.metadata
            ),
            *config.columns,
            *config.constraints,
            *(
                constraint
                for field in config.model_fields.values()
                for constraint in field.construct_table_constraints()
            ),
            schema=config.schema,
            comment=config.comment,
        )
        config.table = table

populate_config_tablename_columns_and_pk(name, new_model)

Sets Model tablename if it's not already set in OrmarConfig. Default tablename if not present is class name lower + s (i.e. Bed becomes -> beds)

Checks if Model's OrmarConfig have primary key and columns set. If not calls the sqlalchemy_columns_from_model_fields to populate columns from ormar.fields definitions.

Parameters:

Name Type Description Default
name str

name of the current Model

required
new_model type[Model]

currently constructed Model

required

Returns:

Type Description
ormar.models.metaclass.ModelMetaclass

Model with populated primary key and columns in OrmarConfig

Raises:

Type Description
ModelDefinitionError

if no field is marked as primary key. Each model has to have pk.

Source code in ormar/models/helpers/sqlalchemy.py
Python
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
def populate_config_tablename_columns_and_pk(
    name: str, new_model: type["Model"]
) -> type["Model"]:
    """
    Sets Model tablename if it's not already set in OrmarConfig.
    Default tablename if not present is class name lower + s (i.e. Bed becomes -> beds)

    Checks if Model's OrmarConfig have primary key and columns set.
    If not calls the sqlalchemy_columns_from_model_fields to populate
    columns from ormar.fields definitions.

    :raises ModelDefinitionError: if no field is marked as primary key.
    Each model has to have pk.

    :param name: name of the current Model
    :type name: str
    :param new_model: currently constructed Model
    :type new_model: ormar.models.metaclass.ModelMetaclass
    :return: Model with populated primary key and columns in OrmarConfig
    :rtype: ormar.models.metaclass.ModelMetaclass
    """
    tablename = name.lower() + "s"
    new_model.ormar_config.tablename = (
        new_model.ormar_config.tablename
        if new_model.ormar_config.tablename
        else tablename
    )
    if new_model.ormar_config.columns:
        # a config with columns is shared with the model that built them
        # (e.g. pydantic copies made for circular references), keep its key
        columns = new_model.ormar_config.columns
        key = new_model.ormar_config.pk
    else:
        pk_fields, columns = sqlalchemy_columns_from_model_fields(
            model_fields=new_model.ormar_config.model_fields, new_model=new_model
        )
        if not pk_fields:
            raise ormar.ModelDefinitionError("Table has to have a primary key.")
        key = build_primary_key(owner=new_model, fields=pk_fields)
        if key.is_composite:
            prepare_composite_key_fields(key=key, columns=columns)

    new_model.ormar_config.columns = columns
    new_model.ormar_config.pk = key
    if not new_model.ormar_config.orders_by:
        # by default, we sort by the primary key if other option not provided
        new_model.ormar_config.orders_by.extend(key.fields)
    return new_model

prepare_composite_key_fields(key, columns)

Validate the fields of a composite primary key and turn off the autoincrement integer keys get by default, as no part of a composite key is generated by the database.

Parameters:

Name Type Description Default
key PrimaryKey

composite primary key of the model

required
columns list[Column]

the model's columns, adjusted in place

required

Raises:

Type Description
ModelDefinitionError

if a part explicitly autoincrements

Source code in ormar/models/helpers/sqlalchemy.py
Python
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
def prepare_composite_key_fields(
    key: "PrimaryKey", columns: list[sqlalchemy.Column]
) -> None:
    """
    Validate the fields of a composite primary key and turn off the
    autoincrement integer keys get by default, as no part of a composite key
    is generated by the database.

    :raises ModelDefinitionError: if a part explicitly autoincrements
    :param key: composite primary key of the model
    :type key: PrimaryKey
    :param columns: the model's columns, adjusted in place
    :type columns: list[sqlalchemy.Column]
    """
    model_fields = key.owner.ormar_config.model_fields
    key_fields = key.fields
    generated = [
        name
        for name in key_fields
        if model_fields[name].autoincrement
        and model_fields[name].autoincrement_explicit
    ]
    if generated:
        raise ormar.ModelDefinitionError(
            f"Composite primary key fields cannot autoincrement: {generated}"
        )
    key_columns = set(key.column_aliases())
    for column in columns:
        if column.name in key_columns:
            column.autoincrement = False

rebuild_primary_key(model)

A key whose only field is a foreign key to a forward reference is built as a key of one column; once the reference resolves to a model with a key of several columns, the foreign key stores several columns and the model's key becomes composite.

Parameters:

Name Type Description Default
model Union[type[Model], type[NewBaseModel]]

model whose forward references were just resolved

required
Source code in ormar/models/helpers/sqlalchemy.py
Python
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
def rebuild_primary_key(model: Union[type["Model"], type["NewBaseModel"]]) -> None:
    """
    A key whose only field is a foreign key to a forward reference is built as
    a key of one column; once the reference resolves to a model with a key of
    several columns, the foreign key stores several columns and the model's
    key becomes composite.

    :param model: model whose forward references were just resolved
    :type model: Union[type[Model], type[NewBaseModel]]
    """
    key = model.ormar_config.pk
    if key.is_composite or len(key.column_aliases()) == 1:
        return
    composite = build_primary_key(owner=cast(type["Model"], model), fields=key.fields)
    model.ormar_config.pk = composite
    model.pk = PkDescriptor(key=composite)  # type: ignore[assignment]

set_constraint_names(config)

Populates the names on IndexColumns and UniqueColumns and CheckColumns constraints.

Parameters:

Name Type Description Default
config OrmarConfig

OrmarConfig of the Model without sqlalchemy table constructed

required
Source code in ormar/models/helpers/sqlalchemy.py
Python
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
def set_constraint_names(config: "OrmarConfig") -> None:
    """
    Populates the names on IndexColumns and UniqueColumns and CheckColumns constraints.

    :param config: OrmarConfig of the Model without sqlalchemy table constructed
    :type config: Model class OrmarConfig
    """
    for constraint in config.constraints:
        if isinstance(constraint, sqlalchemy.UniqueConstraint) and not constraint.name:
            constraint.name = (
                f"uc_{config.tablename}_"
                f"{'_'.join([str(col) for col in constraint._pending_colargs])}"
            )
        elif (
            isinstance(constraint, sqlalchemy.Index)
            and constraint.name == "TEMPORARY_NAME"
        ):
            fragments = (
                index_name_fragment(expression) for expression in constraint.expressions
            )
            constraint.name = sqlalchemy.quoted_name(
                f"ix_{config.tablename}_{'_'.join(fragments)}", quote=None
            )
        elif isinstance(constraint, sqlalchemy.CheckConstraint) and not constraint.name:
            sql_condition: str = str(constraint.sqltext).replace(" ", "_")
            constraint.name = f"check_{config.tablename}_{sql_condition}"

sqlalchemy_columns_from_model_fields(model_fields, new_model)

Iterates over declared on Model model fields and extracts fields that should be treated as database fields.

If the model is empty it sets mandatory id field as primary key (used in through models in m2m relations).

Triggers a validation of relation_names in relation fields. If multiple fields are leading to the same related model only one can have empty related_name param. Also related_names have to be unique.

Sets owner on each model_field as reference to newly created Model.

Parameters:

Name Type Description Default
model_fields dict

dictionary of declared ormar model fields

required
new_model type[Model]
required

Returns:

Type Description
tuple[tuple[str, ...], list[sqlalchemy.Column]]

names of the primary key fields, list of sqlalchemy columns

Raises:

Type Description
ModelDefinitionError

if validation of related_names fail

Source code in ormar/models/helpers/sqlalchemy.py
Python
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
def sqlalchemy_columns_from_model_fields(
    model_fields: dict, new_model: type["Model"]
) -> tuple[tuple[str, ...], list[sqlalchemy.Column]]:
    """
    Iterates over declared on Model model fields and extracts fields that
    should be treated as database fields.

    If the model is empty it sets mandatory id field as primary key
    (used in through models in m2m relations).

    Triggers a validation of relation_names in relation fields. If multiple fields
    are leading to the same related model only one can have empty related_name param.
    Also related_names have to be unique.


    Sets `owner` on each model_field as reference to newly created Model.

    :raises ModelDefinitionError: if validation of related_names fail
    :param model_fields: dictionary of declared ormar model fields
    :type model_fields: dict[str, ormar.Field]
    :param new_model:
    :type new_model: Model class
    :return: names of the primary key fields, list of sqlalchemy columns
    :rtype: tuple[tuple[str, ...], list[sqlalchemy.Column]]
    """
    if len(model_fields.keys()) == 0:
        model_fields["id"] = ormar.Integer(name="id", primary_key=True)
        logging.warning(
            f"Table {new_model.ormar_config.tablename} had no fields so auto "
            "Integer primary key named `id` created."
        )
    validate_related_names_in_relations(model_fields, new_model)
    return _process_fields(model_fields=model_fields, new_model=new_model)

update_column_definition(model, field)

Updates a column with a new type column based on updated parameters in FK fields.

Parameters:

Name Type Description Default
model Union[type[Model], type[NewBaseModel]]

model on which columns needs to be updated

required
field ForeignKeyField

field with column definition that requires update

required

Returns:

Type Description
None

None

Source code in ormar/models/helpers/sqlalchemy.py
Python
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
def update_column_definition(
    model: Union[type["Model"], type["NewBaseModel"]], field: "ForeignKeyField"
) -> None:
    """
    Updates a column with a new type column based on updated parameters in FK fields.

    :param model: model on which columns needs to be updated
    :type model: type["Model"]
    :param field: field with column definition that requires update
    :type field: ForeignKeyField
    :return: None
    :rtype: None
    """
    columns = model.ormar_config.columns
    for ind, column in enumerate(columns):
        if column.name == field.get_alias():
            columns[ind : ind + 1] = field.get_columns()
            columns[:] = merge_shared_columns(model=model, columns=columns)
            break

validate_cross_schema_constraints(metadata, dialect_name)

Rejects cross-schema foreign keys on dialects that cannot enforce them.

SQLite forbids foreign keys whose parent and child tables live in different attached databases. Detecting this at metaclass time is unreliable because models are usually declared before the engine/dialect is known, so the check runs against the assembled metadata before create_all.

Parameters:

Name Type Description Default
metadata MetaData

metadata holding all the registered ormar tables

required
dialect_name str

name of the dialect about to issue DDL

required

Raises:

Type Description
ModelDefinitionError

if a cross-schema FK is found on SQLite

Source code in ormar/models/helpers/sqlalchemy.py
Python
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def validate_cross_schema_constraints(
    metadata: sqlalchemy.MetaData, dialect_name: str
) -> None:
    """
    Rejects cross-schema foreign keys on dialects that cannot enforce them.

    SQLite forbids foreign keys whose parent and child tables live in different
    attached databases. Detecting this at metaclass time is unreliable because
    models are usually declared before the engine/dialect is known, so the check
    runs against the assembled metadata before ``create_all``.

    :raises ModelDefinitionError: if a cross-schema FK is found on SQLite
    :param metadata: metadata holding all the registered ormar tables
    :type metadata: sqlalchemy.MetaData
    :param dialect_name: name of the dialect about to issue DDL
    :type dialect_name: str
    """
    if rules_for(backend_name=dialect_name).cross_schema_foreign_keys:
        return
    for table in metadata.sorted_tables:
        for fk in table.foreign_keys:
            parent_schema = fk.column.table.schema
            child_schema = table.schema
            if parent_schema != child_schema:
                raise ormar.ModelDefinitionError(
                    f"SQLite does not support foreign keys across schemas: "
                    f"{table.fullname}.{fk.parent.name} -> {fk.column.table.fullname}."
                    " Use PostgreSQL or MySQL, or keep both tables in the same schema."
                )

validate_shared_column(model, name, fields, columns)

Check that a column stored by several fields may be shared: at most one of them is a plain field, every foreign key among them names the column itself, and they agree on its type.

Parameters:

Name Type Description Default
model Union[type[Model], type[NewBaseModel]]

model the column belongs to

required
name str

column name

required
fields list[BaseField]

fields storing the column

required
columns list[Column]

the column as each field defines it

required

Raises:

Type Description
ModelDefinitionError

if the fields can't share the column

Source code in ormar/models/helpers/sqlalchemy.py
Python
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
def validate_shared_column(
    model: Union[type["Model"], type["NewBaseModel"]],
    name: str,
    fields: list["BaseField"],
    columns: list[sqlalchemy.Column],
) -> None:
    """
    Check that a column stored by several fields may be shared: at most one
    of them is a plain field, every foreign key among them names the column
    itself, and they agree on its type.

    :raises ModelDefinitionError: if the fields can't share the column
    :param model: model the column belongs to
    :type model: Union[type[Model], type[NewBaseModel]]
    :param name: column name
    :type name: str
    :param fields: fields storing the column
    :type fields: list[BaseField]
    :param columns: the column as each field defines it
    :type columns: list[sqlalchemy.Column]
    """
    plain = [field for field in fields if not field.is_relation]
    relations = [field for field in fields if field.is_relation]
    if len(plain) > 1 or not all(
        names_columns_explicitly(field=field) for field in relations
    ):
        raise ormar.ModelDefinitionError(
            f"Column {name} of {model.__name__} is used by two fields, "
            "rename one of them (name= / columns=)."
        )
    if len({repr(column.type) for column in columns}) > 1:
        raise ormar.ModelDefinitionError(
            f"Fields {[field.name for field in fields]} of {model.__name__} share "
            f"column {name} but give it different types "
            f"({', '.join(repr(column.type) for column in columns)})."
        )