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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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
( |
Source code in ormar/models/helpers/sqlalchemy.py
| Python | |
|---|---|
273 274 275 276 277 278 279 280 281 282 283 284 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |