primary_key
Primary key of a model: which fields form it, their database columns, and how to read, write, bind and compare its value.
Every consumer of a model's key goes through this object instead of a single primary key field name, so the same code serves single-column keys and (later) keys spanning several columns.
CompositeKey
Bases: PrimaryKey
Primary key spanning several columns, whose value is a tuple of the parts in key order.
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
autoincrement
property
Returns:
| Type | Description |
|---|---|
bool
|
always |
is_composite
property
Returns:
| Type | Description |
|---|---|
bool
|
always |
bind_params(value)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
parameters binding the parts to |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
676 677 678 679 680 681 682 683 | |
flat_parts(value)
Turn a key value given as a tuple/list of the key columns or as a dict of exactly the key fields into the raw value of each key column (related models in relation parts replaced by the values they are referenced by).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
tuple[Any, ...]
|
the value of each key column, in key order |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if the value does not match the key or a column gets a tuple, list, dict or model instead of a single value |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |
getter(keys)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
tuple[str, ...]
|
names under which the key parts are stored, in key order |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Mapping[str, Any]], Any]
|
reader returning the key tuple or |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
658 659 660 661 662 663 664 665 | |
mismatch_error(value)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value that doesn't match the key |
required |
Returns:
| Type | Description |
|---|---|
QueryDefinitionError
|
error describing the expected key value |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
622 623 624 625 626 627 628 629 630 631 632 633 | |
python_type()
Returns:
| Type | Description |
|---|---|
Any
|
|
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
542 543 544 545 546 547 | |
slot_names(count)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
number of key columns |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
|
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
667 668 669 670 671 672 673 674 | |
split_value(value)
flat_parts without checking that each column gets a single value,
for key values ormar built itself (e.g. a model's pk).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
tuple[Any, ...]
|
the value of each key column, in key order |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if the value does not match the key |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
to_dict(value)
Split a key value given as a tuple/list of the key columns or as a dict of exactly the key fields into the value of each key field: a foreign key stored in several columns gets the tuple of its columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
key parts by field name |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if the value does not match the key |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
value(instance)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
NewBaseModel
|
model instance |
required |
Returns:
| Type | Description |
|---|---|
Any
|
tuple of the key parts, |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
549 550 551 552 553 554 555 556 | |
value_from(data)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, Any]
|
values by field name (e.g. a model dict) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
tuple of the key parts, |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 | |
CompositeKeyGetter
Reads a composite key out of a mapping: a tuple of the parts, or None
when any part is None.
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
__call__(data)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, Any]
|
mapping holding the key parts |
required |
Returns:
| Type | Description |
|---|---|
Any
|
tuple of the parts, |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
507 508 509 510 511 512 513 514 515 | |
__init__(*, keys)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
tuple[str, ...]
|
names under which the key parts are stored, in key order |
required |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
500 501 502 503 504 505 | |
KeyLayout
dataclass
The columns of a primary key, resolved once (and again after the owner's column caches are reset, e.g. when forward references resolve).
Attributes:
| Name | Type | Description |
|---|---|---|
key |
PrimaryKey
|
primary key the layout belongs to |
column_aliases |
tuple[str, ...]
|
database column names in key order |
column_fields |
tuple[str, ...]
|
per key column, the first key field storing it |
simple |
bool
|
whether every key field stores exactly one column of its own (no foreign key of several columns, no shared column), so the key value has one part per key field |
bind_slots |
tuple[str, ...]
|
names of the bind parameters of the key columns |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
PrimaryKey
Bases: ABC
Primary key of a model class.
Column names are resolved from the owner once (layout) and cached
until the owner's column caches are reset. Inheritance and proxies get
their own key (rebind), and a cached layout is used only by the key
it was built for, so a key copied together with a config cannot point
at another model's table.
Attributes:
| Name | Type | Description |
|---|---|---|
owner |
model class the key belongs to |
|
fields |
ormar field names forming the key, in key order |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
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 108 109 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 175 176 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 206 207 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 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 340 341 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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
autoincrement
property
Returns:
| Type | Description |
|---|---|
bool
|
whether every key field is generated by the database |
is_composite
abstractmethod
property
Returns:
| Type | Description |
|---|---|
bool
|
whether the key spans more than one column |
__init__(*, owner, fields)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner
|
type[Model]
|
model class the key belongs to |
required |
fields
|
tuple[str, ...]
|
ormar field names forming the key, in key order |
required |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
bind_params(value)
abstractmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
parameters binding the value to |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
389 390 391 392 393 394 395 396 | |
bind_slots()
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
names of the bind parameters the key is bound to in writes |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
226 227 228 229 230 231 | |
build_layout()
Resolve the key's database columns: the columns of each key field in
key order (all link columns of a foreign key), each column several
key fields share (e.g. a tenant_id of two foreign keys) listed
once, where it first appears.
Returns:
| Type | Description |
|---|---|
KeyLayout
|
the key's columns |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
column_aliases()
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
database column names of the key, in key order |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
202 203 204 205 206 207 | |
column_parts(data)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
an instance of the owner, or its values by field name (e.g. a model dict) |
required |
Returns:
| Type | Description |
|---|---|
tuple[Any, ...]
|
the value of each key column, |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
188 189 190 191 192 193 194 195 196 197 198 199 200 | |
columns()
Returns:
| Type | Description |
|---|---|
tuple[sqlalchemy.Column, ...]
|
SQLAlchemy columns of the key, in key order |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
209 210 211 212 213 214 215 | |
expand_pk_kwarg(kwargs)
Replace a pk keyword with the key fields it stands for::
| Text Only | |
|---|---|
1 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
dict[str, Any]
|
keyword arguments, possibly holding |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
new keyword arguments without |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
flat_parts(value)
abstractmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
tuple[Any, ...]
|
the raw value of each key column, in key order |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
380 381 382 383 384 385 386 387 | |
getter(keys)
abstractmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
tuple[str, ...]
|
names under which the key parts are stored in a mapping (field names in a model dict, column names in a row) |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Mapping[str, Any]], Any]
|
callable reading the key value out of such a mapping |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
350 351 352 353 354 355 356 357 358 | |
in_clause(*, slot)
Build the condition matching the rows of a list of keys bound to one expanding parameter, one value per row (a tuple for a composite key)::
| Text Only | |
|---|---|
1 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slot
|
str
|
bind parameter name |
required |
Returns:
| Type | Description |
|---|---|
sqlalchemy.ColumnElement
|
condition over the key columns |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
item_reader()
Returns:
| Type | Description |
|---|---|
Callable[[Mapping[str, Any]], Any]
|
callable reading the key value out of a model dict (values by field name, a foreign key stored in several columns holding the tuple of its columns) |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
360 361 362 363 364 365 366 367 368 369 | |
layout()
Returns:
| Type | Description |
|---|---|
KeyLayout
|
the key's columns, built once and cached until the owner's column caches are reset (not cached while a relation part still points at a forward reference) |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
lookup(kwargs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
dict[str, Any]
|
keyword arguments by field name |
required |
Returns:
| Type | Description |
|---|---|
Optional[dict[str, Any]]
|
the key fields and their values, |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
304 305 306 307 308 309 310 311 312 313 314 | |
part_type(field)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
str
|
key field name |
required |
Returns:
| Type | Description |
|---|---|
Any
|
python type of the key part |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
116 117 118 119 120 121 122 123 124 125 126 | |
part_value(field, value)
Turn the value a model holds for a key field into the key part: a related model in a relation part is replaced by the value its foreign key stores (e.g. the related model's pk), raw values pass through.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
str
|
key field name |
required |
value
|
Any
|
value held for the field |
required |
Returns:
| Type | Description |
|---|---|
Any
|
key part |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
python_type()
abstractmethod
Returns:
| Type | Description |
|---|---|
Any
|
python type of the key value |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
325 326 327 328 329 330 | |
rebind(*, owner)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner
|
type[Model]
|
model class the new key belongs to |
required |
Returns:
| Type | Description |
|---|---|
PrimaryKey
|
the same key for another model class (inheritance, proxies) |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
316 317 318 319 320 321 322 323 | |
row_value()
Returns:
| Type | Description |
|---|---|
sqlalchemy.ColumnElement
|
the key column, or the key columns as a tuple for a composite
key, to compare whole keys: |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
261 262 263 264 265 266 267 268 | |
slot_names(count)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
number of key columns |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
names of the bind parameters the key is bound to in writes |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
179 180 181 182 183 184 185 186 | |
to_dict(value)
abstractmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
key value split into its fields, by field name |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
341 342 343 344 345 346 347 348 | |
value(instance)
abstractmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
NewBaseModel
|
model instance |
required |
Returns:
| Type | Description |
|---|---|
Any
|
key value of the instance |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
332 333 334 335 336 337 338 339 | |
value_from(data)
abstractmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, Any]
|
values by field name (e.g. a model dict) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
key value held in the mapping, |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
371 372 373 374 375 376 377 378 | |
where_clause(*, slots, typed=True)
Build the condition matching one row by key, each key column compared with a named bind parameter::
| Text Only | |
|---|---|
1 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slots
|
tuple[str, ...]
|
bind parameter names, one per key column |
required |
typed
|
bool
|
give each bind its column's type, so values get the column's bind processing (e.g. UUID) |
True
|
Returns:
| Type | Description |
|---|---|
sqlalchemy.ColumnElement
|
condition over the key columns |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
SingleColumnKey
Bases: PrimaryKey
Primary key of one column, whose value is a plain scalar.
Attributes:
| Name | Type | Description |
|---|---|---|
field |
name of the key field |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
is_composite
property
Returns:
| Type | Description |
|---|---|
bool
|
always |
__init__(*, owner, fields)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner
|
type[Model]
|
model class the key belongs to |
required |
fields
|
tuple[str, ...]
|
the one key field name |
required |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
406 407 408 409 410 411 412 413 414 | |
bind_params(value)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
482 483 484 485 486 487 488 489 | |
flat_parts(value)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
tuple[Any, ...]
|
|
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
473 474 475 476 477 478 479 480 | |
getter(keys)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
tuple[str, ...]
|
one name under which the key is stored |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Mapping[str, Any]], Any]
|
|
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
452 453 454 455 456 457 458 459 | |
python_type()
Returns:
| Type | Description |
|---|---|
Any
|
python type of the key field |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
424 425 426 427 428 429 | |
to_dict(value)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
key value |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
443 444 445 446 447 448 449 450 | |
value(instance)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
NewBaseModel
|
model instance |
required |
Returns:
| Type | Description |
|---|---|
Any
|
value of the key field, |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
431 432 433 434 435 436 437 438 439 440 441 | |
value_from(data)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, Any]
|
values by field name (e.g. a model dict) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
value of the key field, |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
461 462 463 464 465 466 467 468 469 470 471 | |
build_primary_key(*, owner, fields)
Create the primary key of a model from the fields marked as primary key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner
|
type[Model]
|
model class the key belongs to |
required |
fields
|
tuple[str, ...]
|
ormar field names marked as primary key, in declaration order |
required |
Returns:
| Type | Description |
|---|---|
PrimaryKey
|
primary key of the model |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | |
not_scalar(parts)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parts
|
tuple[Any, ...]
|
values of the key columns |
required |
Returns:
| Type | Description |
|---|---|
bool
|
whether any column got a tuple, list, dict or model instead of a single value |
Source code in ormar/models/primary_key.py
| Python | |
|---|---|
51 52 53 54 55 56 57 58 59 60 61 62 | |