1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
53
54
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
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
397
398
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
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
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
|
:orphan:
.. _glossary:
========
Glossary
========
.. glossary::
:sorted:
1.x style
2.0 style
1.x-style
2.0-style
These terms are new in SQLAlchemy 1.4 and refer to the SQLAlchemy 1.4->
2.0 transition plan, described at :ref:`migration_20_toplevel`. The
term "1.x style" refers to an API used in the way it's been documented
throughout the 1.x series of SQLAlchemy and earlier (e.g. 1.3, 1.2, etc)
and the term "2.0 style" refers to the way an API will look in version
2.0. Version 1.4 implements nearly all of 2.0's API in so-called
"transition mode", while version 2.0 still maintains the legacy
:class:`_orm.Query` object to allow legacy code to remain largely
2.0 compatible.
.. seealso::
:ref:`migration_20_toplevel`
sentinel
insert sentinel
This is a SQLAlchemy-specific term that refers to a
:class:`_schema.Column` which can be used for a bulk
:term:`insertmanyvalues` operation to track INSERTed data records
against rows passed back using RETURNING or similar. Such a
column configuration is necessary for those cases when the
:term:`insertmanyvalues` feature does an optimized INSERT..RETURNING
statement for many rows at once while still being able to guarantee the
order of returned rows matches the input data.
For typical use cases, the SQLAlchemy SQL compiler can automatically
make use of surrogate integer primary key columns as "insert
sentinels", and no user-configuration is required. For less common
cases with other varieties of server-generated primary key values,
explicit "insert sentinel" columns may be optionally configured within
:term:`table metadata` in order to optimize INSERT statements that
are inserting many rows at once.
.. seealso::
:ref:`engine_insertmanyvalues_returning_order` - in the section
:ref:`engine_insertmanyvalues`
insertmanyvalues
This refers to a SQLAlchemy-specific feature which allows INSERT
statements to emit thousands of new rows within a single statement
while at the same time allowing server generated values to be returned
inline from the statement using RETURNING or similar, for performance
optimization purposes. The feature is intended to be transparently
available for selected backends, but does offer some configurational
options. See the section :ref:`engine_insertmanyvalues` for a full
description of this feature.
.. seealso::
:ref:`engine_insertmanyvalues`
mixin class
mixin classes
A common object-oriented pattern where a class that contains methods or
attributes for use by other classes without having to be the parent class
of those other classes.
.. seealso::
`Mixin (via Wikipedia) <https://en.wikipedia.org/wiki/Mixin>`_
reflection
reflected
In SQLAlchemy, this term refers to the feature of querying a database's
schema catalogs in order to load information about existing tables,
columns, constraints, and other constructs. SQLAlchemy includes
features that can both provide raw data for this information, as well
as that it can construct Core/ORM usable :class:`.Table` objects
from database schema catalogs automatically.
.. seealso::
:ref:`metadata_reflection_toplevel` - complete background on
database reflection.
:ref:`orm_declarative_reflected` - background on integrating
ORM mappings with reflected tables.
imperative
declarative
In the SQLAlchemy ORM, these terms refer to two different styles of
mapping Python classes to database tables.
.. seealso::
:ref:`orm_declarative_mapping`
:ref:`orm_imperative_mapping`
facade
An object that serves as a front-facing interface masking more complex
underlying or structural code.
.. seealso::
`Facade pattern (via Wikipedia) <https://en.wikipedia.org/wiki/Facade_pattern>`_
relational
relational algebra
An algebraic system developed by Edgar F. Codd that is used for
modelling and querying the data stored in relational databases.
.. seealso::
`Relational Algebra (via Wikipedia) <https://en.wikipedia.org/wiki/Relational_algebra>`_
cartesian product
Given two sets A and B, the cartesian product is the set of all ordered pairs (a, b)
where a is in A and b is in B.
In terms of SQL databases, a cartesian product occurs when we select from two
or more tables (or other subqueries) without establishing any kind of criteria
between the rows of one table to another (directly or indirectly). If we
SELECT from table A and table B at the same time, we get every row of A matched
to the first row of B, then every row of A matched to the second row of B, and
so on until every row from A has been paired with every row of B.
Cartesian products cause enormous result sets to be generated and can easily
crash a client application if not prevented.
.. seealso::
`Cartesian Product (via Wikipedia) <https://en.wikipedia.org/wiki/Cartesian_product>`_
cyclomatic complexity
A measure of code complexity based on the number of possible paths
through a program's source code.
.. seealso::
`Cyclomatic Complexity <https://en.wikipedia.org/wiki/Cyclomatic_complexity>`_
bound parameter
bound parameters
bind parameter
bind parameters
Bound parameters are the primary means in which data is passed to the
:term:`DBAPI` database driver. While the operation to be invoked is
based on the SQL statement string, the data values themselves are
passed separately, where the driver contains logic that will safely
process these strings and pass them to the backend database server,
which may either involve formatting the parameters into the SQL string
itself, or passing them to the database using separate protocols.
The specific system by which the database driver does this should not
matter to the caller; the point is that on the outside, data should
**always** be passed separately and not as part of the SQL string
itself. This is integral both to having adequate security against
SQL injections as well as allowing the driver to have the best
performance.
.. seealso::
`Prepared Statement <https://en.wikipedia.org/wiki/Prepared_statement>`_ - at Wikipedia
`bind parameters <https://use-the-index-luke.com/sql/where-clause/bind-parameters>`_ - at Use The Index, Luke!
:ref:`tutorial_sending_parameters` - in the :ref:`unified_tutorial`
selectable
A term used in SQLAlchemy to describe a SQL construct that represents
a collection of rows. It's largely similar to the concept of a
"relation" in :term:`relational algebra`. In SQLAlchemy, objects
that subclass the :class:`_expression.Selectable` class are considered to be
usable as "selectables" when using SQLAlchemy Core. The two most
common constructs are that of the :class:`_schema.Table` and that of the
:class:`_expression.Select` statement.
ORM-annotated
annotations
The phrase "ORM-annotated" refers to an internal aspect of SQLAlchemy,
where a Core object such as a :class:`_schema.Column` object can carry along
additional runtime information that marks it as belonging to a particular
ORM mapping. The term should not be confused with the common phrase
"type annotation", which refers to Python source code "type hints" used
for static typing as introduced at :pep:`484`.
Most of SQLAlchemy's documented code examples are formatted with a
small note regarding "Annotated Example" or "Non-annotated Example".
This refers to whether or not the example is :pep:`484` annotated,
and is not related to the SQLAlchemy concept of "ORM-annotated".
When the phrase "ORM-annotated" appears in documentation, it is
referring to Core SQL expression objects such as :class:`.Table`,
:class:`.Column`, and :class:`.Select` objects, which originate from,
or refer to sub-elements that originate from, one or more ORM mappings,
and therefore will have ORM-specific interpretations and/or behaviors
when passed to ORM methods such as :meth:`_orm.Session.execute`.
For example, when we construct a :class:`.Select` object from an ORM
mapping, such as the ``User`` class illustrated in the
:ref:`ORM Tutorial <tutorial_declaring_mapped_classes>`::
>>> stmt = select(User)
The internal state of the above :class:`.Select` refers to the
:class:`.Table` to which ``User`` is mapped. The ``User`` class
itself is not immediately referenced. This is how the :class:`.Select`
construct remains compatible with Core-level processes (note that
the ``._raw_columns`` member of :class:`.Select` is private and
should not be accessed by end-user code)::
>>> stmt._raw_columns
[Table('user_account', MetaData(), Column('id', Integer(), ...)]
However, when our :class:`.Select` is passed along to an ORM
:class:`.Session`, the ORM entities that are indirectly associated
with the object are used to interpret this :class:`.Select` in an
ORM context. The actual "ORM annotations" can be seen in another
private variable ``._annotations``::
>>> stmt._raw_columns[0]._annotations
immutabledict({
'entity_namespace': <Mapper at 0x7f4dd8098c10; User>,
'parententity': <Mapper at 0x7f4dd8098c10; User>,
'parentmapper': <Mapper at 0x7f4dd8098c10; User>
})
Therefore we refer to ``stmt`` as an **ORM-annotated select()** object.
It's a :class:`.Select` statement that contains additional information
that will cause it to be interpreted in an ORM-specific way when passed
to methods like :meth:`_orm.Session.execute`.
plugin
plugin-enabled
plugin-specific
"plugin-enabled" or "plugin-specific" generally indicates a function or method in
SQLAlchemy Core which will behave differently when used in an ORM
context.
SQLAlchemy allows Core constructs such as :class:`_sql.Select` objects
to participate in a "plugin" system, which can inject additional
behaviors and features into the object that are not present by default.
Specifically, the primary "plugin" is the "orm" plugin, which is
at the base of the system that the SQLAlchemy ORM makes use of
Core constructs in order to compose and execute SQL queries that
return ORM results.
.. seealso::
:ref:`migration_20_unify_select`
crud
CRUD
An acronym meaning "Create, Update, Delete". The term in SQL refers to the
set of operations that create, modify and delete data from the database,
also known as :term:`DML`, and typically refers to the ``INSERT``,
``UPDATE``, and ``DELETE`` statements.
executemany
This term refers to a part of the :pep:`249` DBAPI specification
indicating a single SQL statement that may be invoked against a
database connection with multiple parameter sets. The specific
method is known as
`cursor.executemany() <https://peps.python.org/pep-0249/#executemany>`_,
and it has many behavioral differences in comparison to the
`cursor.execute() <https://peps.python.org/pep-0249/#execute>`_
method which is used for single-statement invocation. The "executemany"
method executes the given SQL statement multiple times, once for
each set of parameters passed. The general rationale for using
executemany is that of improved performance, wherein the DBAPI may
use techniques such as preparing the statement just once beforehand,
or otherwise optimizing for invoking the same statement many times.
SQLAlchemy typically makes use of the ``cursor.executemany()`` method
automatically when the :meth:`_engine.Connection.execute` method is
used where a list of parameter dictionaries were passed; this indicates
to SQLAlchemy Core that the SQL statement and processed parameter sets
should be passed to ``cursor.executemany()``, where the statement will
be invoked by the driver for each parameter dictionary individually.
A key limitation of the ``cursor.executemany()`` method as used with
all known DBAPIs is that the ``cursor`` is not configured to return
rows when this method is used. For **most** backends (a notable
exception being the cx_Oracle, / OracleDB DBAPIs), this means that
statements like ``INSERT..RETURNING`` typically cannot be used with
``cursor.executemany()`` directly, since DBAPIs typically do not
aggregate the single row from each INSERT execution together.
To overcome this limitation, SQLAlchemy as of the 2.0 series implements
an alternative form of "executemany" which is referred towards as
:ref:`engine_insertmanyvalues`. This feature makes use of
``cursor.execute()`` to invoke an INSERT statement that will proceed
with multiple parameter sets in one round trip, thus producing the same
effect as using ``cursor.executemany()`` while still supporting
RETURNING.
.. seealso::
:ref:`tutorial_multiple_parameters` - tutorial introduction to
"executemany"
:ref:`engine_insertmanyvalues` - SQLAlchemy feature which allows
RETURNING to be used with "executemany"
marshalling
data marshalling
The process of transforming the memory representation of an object to
a data format suitable for storage or transmission to another part of
a system, when data must be moved between different parts of a
computer program or from one program to another. In terms of
SQLAlchemy, we often need to "marshal" data into a format appropriate
for passing into the relational database.
.. seealso::
`Marshalling (via Wikipedia) <https://en.wikipedia.org/wiki/Marshalling_(computer_science)>`_
:ref:`types_typedecorator` - SQLAlchemy's :class:`.TypeDecorator`
is commonly used for data marshalling as data is sent into the
database for INSERT and UPDATE statements, and "unmarshalling"
data as it is retrieved using SELECT statements.
descriptor
descriptors
In Python, a descriptor is an object attribute with “binding behavior”,
one whose attribute access has been overridden by methods in the
`descriptor protocol <https://docs.python.org/howto/descriptor.html>`_.
Those methods are ``__get__()``, ``__set__()``, and ``__delete__()``.
If any of those methods are defined for an object, it is said to be a
descriptor.
In SQLAlchemy, descriptors are used heavily in order to provide attribute behavior
on mapped classes. When a class is mapped as such::
class MyClass(Base):
__tablename__ = "foo"
id = Column(Integer, primary_key=True)
data = Column(String)
The ``MyClass`` class will be :term:`mapped` when its definition
is complete, at which point the ``id`` and ``data`` attributes,
starting out as :class:`_schema.Column` objects, will be replaced
by the :term:`instrumentation` system with instances
of :class:`.InstrumentedAttribute`, which are descriptors that
provide the above mentioned ``__get__()``, ``__set__()`` and
``__delete__()`` methods. The :class:`.InstrumentedAttribute`
will generate a SQL expression when used at the class level:
.. sourcecode:: pycon+sql
>>> print(MyClass.data == 5)
{printsql}data = :data_1
and at the instance level, keeps track of changes to values,
and also :term:`lazy loads` unloaded attributes
from the database::
>>> m1 = MyClass()
>>> m1.id = 5
>>> m1.data = "some data"
>>> from sqlalchemy import inspect
>>> inspect(m1).attrs.data.history.added
"some data"
DDL
An acronym for **Data Definition Language**. DDL is the subset
of SQL that relational databases use to configure tables, constraints,
and other permanent objects within a database schema. SQLAlchemy
provides a rich API for constructing and emitting DDL expressions.
.. seealso::
:ref:`metadata_toplevel`
`DDL (via Wikipedia) <https://en.wikipedia.org/wiki/Data_definition_language>`_
:term:`DML`
:term:`DQL`
DML
An acronym for **Data Manipulation Language**. DML is the subset of
SQL that relational databases use to *modify* the data in tables. DML
typically refers to the three widely familiar statements of INSERT,
UPDATE and DELETE, otherwise known as :term:`CRUD` (acronym for "Create,
Read, Update, Delete").
.. seealso::
`DML (via Wikipedia) <https://en.wikipedia.org/wiki/Data_manipulation_language>`_
:term:`DDL`
:term:`DQL`
DQL
An acronym for **Data Query Language**. DQL is the subset of
SQL that relational databases use to *read* the data in tables.
DQL almost exclusively refers to the SQL SELECT construct as the
top level SQL statement in use.
.. seealso::
`DQL (via Wikipedia) <https://en.wikipedia.org/wiki/Data_query_language>`_
:term:`DML`
:term:`DDL`
metadata
database metadata
table metadata
The term "metadata" generally refers to "data that describes data";
data that itself represents the format and/or structure of some other
kind of data. In SQLAlchemy, the term "metadata" typically refers to
the :class:`_schema.MetaData` construct, which is a collection of information
about the tables, columns, constraints, and other :term:`DDL` objects
that may exist in a particular database.
.. seealso::
`Metadata Mapping (via Martin Fowler) <https://www.martinfowler.com/eaaCatalog/metadataMapping.html>`_
:ref:`tutorial_working_with_metadata` - in the :ref:`unified_tutorial`
version id column
In SQLAlchemy, this refers to the use of a particular table column that
tracks the "version" of a particular row, as the row changes values. While
there are different kinds of relational patterns that make use of a
"version id column" in different ways, SQLAlchemy's ORM includes a particular
feature that allows for such a column to be configured as a means of
testing for stale data when a row is being UPDATEd with new information.
If the last known "version" of this column does not match that of the
row when we try to put new data into the row, we know that we are
acting on stale information.
There are also other ways of storing "versioned" rows in a database,
often referred to as "temporal" data. In addition to SQLAlchemy's
versioning feature, a few more examples are also present in the
documentation, see the links below.
.. seealso::
:ref:`mapper_version_counter` - SQLAlchemy's built-in version id feature.
:ref:`examples_versioning` - other examples of mappings that version rows
temporally.
registry
An object, typically globally accessible, that contains long-lived
information about some program state that is generally useful to many
parts of a program.
.. seealso::
`Registry (via Martin Fowler) <https://martinfowler.com/eaaCatalog/registry.html>`_
cascade
A term used in SQLAlchemy to describe how an ORM persistence action that
takes place on a particular object would extend into other objects
which are directly associated with that object. In SQLAlchemy, these
object associations are configured using the :func:`_orm.relationship`
construct. :func:`_orm.relationship` contains a parameter called
:paramref:`_orm.relationship.cascade` which provides options on how certain
persistence operations may cascade.
The term "cascades" as well as the general architecture of this system
in SQLAlchemy was borrowed, for better or worse, from the Hibernate
ORM.
.. seealso::
:ref:`unitofwork_cascades`
dialect
In SQLAlchemy, the "dialect" is a Python object that represents information
and methods that allow database operations to proceed on a particular
kind of database backend and a particular kind of Python driver (or
:term:`DBAPI`) for that database. SQLAlchemy dialects are subclasses
of the :class:`.Dialect` class.
.. seealso::
:ref:`engines_toplevel`
discriminator
A result-set column which is used during :term:`polymorphic` loading
to determine what kind of mapped class should be applied to a particular
incoming result row.
.. seealso::
:ref:`inheritance_toplevel`
instrumentation
instrumented
instrumenting
Instrumentation refers to the process of augmenting the functionality
and attribute set of a particular class. Ideally, the
behavior of the class should remain close to a regular
class, except that additional behaviors and features are
made available. The SQLAlchemy :term:`mapping` process,
among other things, adds database-enabled :term:`descriptors`
to a mapped
class each of which represents a particular database column
or relationship to a related class.
identity key
A key associated with ORM-mapped objects that identifies their
primary key identity within the database, as well as their unique
identity within a :class:`_orm.Session` :term:`identity map`.
In SQLAlchemy, you can view the identity key for an ORM object
using the :func:`_sa.inspect` API to return the :class:`_orm.InstanceState`
tracking object, then looking at the :attr:`_orm.InstanceState.key`
attribute::
>>> from sqlalchemy import inspect
>>> inspect(some_object).key
(<class '__main__.MyTable'>, (1,), None)
.. seealso::
:term:`identity map`
identity map
A mapping between Python objects and their database identities.
The identity map is a collection that's associated with an
ORM :term:`Session` object, and maintains a single instance
of every database object keyed to its identity. The advantage
to this pattern is that all operations which occur for a particular
database identity are transparently coordinated onto a single
object instance. When using an identity map in conjunction with
an :term:`isolated` transaction, having a reference
to an object that's known to have a particular primary key can
be considered from a practical standpoint to be a
proxy to the actual database row.
.. seealso::
`Identity Map (via Martin Fowler) <https://martinfowler.com/eaaCatalog/identityMap.html>`_
:ref:`session_get` - how to look up an object in the identity map
by primary key
lazy initialization
A tactic of delaying some initialization action, such as creating objects,
populating data, or establishing connectivity to other services, until
those resources are required.
.. seealso::
`Lazy initialization (via Wikipedia) <https://en.wikipedia.org/wiki/Lazy_initialization>`_
lazy load
lazy loads
lazy loaded
lazy loading
In object relational mapping, a "lazy load" refers to an
attribute that does not contain its database-side value
for some period of time, typically when the object is
first loaded. Instead, the attribute receives a
*memoization* that causes it to go out to the database
and load its data when it's first used. Using this pattern,
the complexity and time spent within object fetches can
sometimes be reduced, in that
attributes for related tables don't need to be addressed
immediately.
Lazy loading is the opposite of :term:`eager loading`.
Within SQLAlchemy, lazy loading is a key feature of the ORM, and
applies to attributes which are :term:`mapped` on a user-defined class.
When attributes that refer to database columns or related objects
are accessed, for which no loaded value is present, the ORM makes
use of the :class:`_orm.Session` for which the current object is
associated with in the :term:`persistent` state, and emits a SELECT
statement on the current transaction, starting a new transaction if
one was not in progress. If the object is in the :term:`detached`
state and not associated with any :class:`_orm.Session`, this is
considered to be an error state and an
:ref:`informative exception <error_bhk3>` is raised.
.. seealso::
`Lazy Load (via Martin Fowler) <https://martinfowler.com/eaaCatalog/lazyLoad.html>`_
:term:`N plus one problem`
:ref:`loading_columns` - includes information on lazy loading of
ORM mapped columns
:doc:`orm/queryguide/relationships` - includes information on lazy
loading of ORM related objects
:ref:`asyncio_orm_avoid_lazyloads` - tips on avoiding lazy loading
when using the :ref:`asyncio_toplevel` extension
eager load
eager loads
eager loaded
eager loading
eagerly load
In object relational mapping, an "eager load" refers to an attribute
that is populated with its database-side value at the same time as when
the object itself is loaded from the database. In SQLAlchemy, the term
"eager loading" usually refers to related collections and instances of
objects that are linked between mappings using the
:func:`_orm.relationship` construct, but can also refer to additional
column attributes being loaded, often from other tables related to a
particular table being queried, such as when using
:ref:`inheritance <inheritance_toplevel>` mappings.
Eager loading is the opposite of :term:`lazy loading`.
.. seealso::
:doc:`orm/queryguide/relationships`
mapping
mapped
mapped class
ORM mapped class
We say a class is "mapped" when it has been associated with an
instance of the :class:`_orm.Mapper` class. This process associates
the class with a database table or other :term:`selectable` construct,
so that instances of it can be persisted and loaded using a
:class:`.Session`.
.. seealso::
:ref:`orm_mapping_classes_toplevel`
N plus one problem
N plus one
The N plus one problem is a common side effect of the
:term:`lazy load` pattern, whereby an application wishes
to iterate through a related attribute or collection on
each member of a result set of objects, where that
attribute or collection is set to be loaded via the lazy
load pattern. The net result is that a SELECT statement
is emitted to load the initial result set of parent objects;
then, as the application iterates through each member,
an additional SELECT statement is emitted for each member
in order to load the related attribute or collection for
that member. The end result is that for a result set of
N parent objects, there will be N + 1 SELECT statements emitted.
The N plus one problem is alleviated using :term:`eager loading`.
.. seealso::
:ref:`tutorial_orm_loader_strategies`
:doc:`orm/queryguide/relationships`
polymorphic
polymorphically
Refers to a function that handles several types at once. In SQLAlchemy,
the term is usually applied to the concept of an ORM mapped class
whereby a query operation will return different subclasses
based on information in the result set, typically by checking the
value of a particular column in the result known as the :term:`discriminator`.
Polymorphic loading in SQLAlchemy implies that a one or a
combination of three different schemes are used to map a hierarchy
of classes; "joined", "single", and "concrete". The section
:ref:`inheritance_toplevel` describes inheritance mapping fully.
method chaining
generative
"Method chaining", referred to within SQLAlchemy documentation as
"generative", is an object-oriented technique whereby the state of an
object is constructed by calling methods on the object. The object
features any number of methods, each of which return a new object (or
in some cases the same object) with additional state added to the
object.
The two SQLAlchemy objects that make the most use of
method chaining are the :class:`_expression.Select`
object and the :class:`.orm.query.Query` object.
For example, a :class:`_expression.Select` object can
be assigned two expressions to its WHERE clause as well
as an ORDER BY clause by calling upon the :meth:`_expression.Select.where`
and :meth:`_expression.Select.order_by` methods::
stmt = (
select(user.c.name)
.where(user.c.id > 5)
.where(user.c.name.like("e%"))
.order_by(user.c.name)
)
Each method call above returns a copy of the original
:class:`_expression.Select` object with additional qualifiers
added.
release
releases
released
In the context of SQLAlchemy, the term "released"
refers to the process of ending the usage of a particular
database connection. SQLAlchemy features the usage
of connection pools, which allows configurability as to
the lifespan of database connections. When using a pooled
connection, the process of "closing" it, i.e. invoking
a statement like ``connection.close()``, may have the effect
of the connection being returned to an existing pool,
or it may have the effect of actually shutting down the
underlying TCP/IP connection referred to by that connection -
which one takes place depends on configuration as well
as the current state of the pool. So we used the term
*released* instead, to mean "do whatever it is you do
with connections when we're done using them".
The term will sometimes be used in the phrase, "release
transactional resources", to indicate more explicitly that
what we are actually "releasing" is any transactional
state which as accumulated upon the connection. In most
situations, the process of selecting from tables, emitting
updates, etc. acquires :term:`isolated` state upon
that connection as well as potential row or table locks.
This state is all local to a particular transaction
on the connection, and is released when we emit a rollback.
An important feature of the connection pool is that when
we return a connection to the pool, the ``connection.rollback()``
method of the DBAPI is called as well, so that as the
connection is set up to be used again, it's in a "clean"
state with no references held to the previous series
of operations.
.. seealso::
:ref:`pooling_toplevel`
DBAPI
pep-249
DBAPI is shorthand for the phrase "Python Database API
Specification". This is a widely used specification
within Python to define common usage patterns for all
database connection packages. The DBAPI is a "low level"
API which is typically the lowest level system used
in a Python application to talk to a database. SQLAlchemy's
:term:`dialect` system is constructed around the
operation of the DBAPI, providing individual dialect
classes which service a specific DBAPI on top of a
specific database engine; for example, the :func:`_sa.create_engine`
URL ``postgresql+psycopg2://@localhost/test``
refers to the :mod:`psycopg2 <.postgresql.psycopg2>`
DBAPI/dialect combination, whereas the URL ``mysql+mysqldb://@localhost/test``
refers to the :mod:`MySQL for Python <.mysql.mysqldb>`
DBAPI/dialect combination.
.. seealso::
`PEP 249 - Python Database API Specification v2.0 <https://www.python.org/dev/peps/pep-0249/>`_
domain model
A domain model in problem solving and software engineering is a conceptual model of all the topics related to a specific problem. It describes the various entities, their attributes, roles, and relationships, plus the constraints that govern the problem domain.
(via Wikipedia)
.. seealso::
`Domain Model (via Wikipedia) <https://en.wikipedia.org/wiki/Domain_model>`_
unit of work
A software architecture where a persistence system such as an object
relational mapper maintains a list of changes made to a series of
objects, and periodically flushes all those pending changes out to the
database.
SQLAlchemy's :class:`_orm.Session` implements the unit of work pattern,
where objects that are added to the :class:`_orm.Session` using methods
like :meth:`_orm.Session.add` will then participate in unit-of-work
style persistence.
For a walk-through of what unit of work persistence looks like in
SQLAlchemy, start with the section :ref:`tutorial_orm_data_manipulation`
in the :ref:`unified_tutorial`. Then for more detail, see
:ref:`session_basics` in the general reference documentation.
.. seealso::
`Unit of Work (via Martin Fowler) <https://martinfowler.com/eaaCatalog/unitOfWork.html>`_
:ref:`tutorial_orm_data_manipulation`
:ref:`session_basics`
expire
expired
expires
expiring
Expiring
In the SQLAlchemy ORM, refers to when the data in a :term:`persistent`
or sometimes :term:`detached` object is erased, such that when
the object's attributes are next accessed, a :term:`lazy load` SQL
query will be emitted in order to refresh the data for this object
as stored in the current ongoing transaction.
.. seealso::
:ref:`session_expire`
Session
The container or scope for ORM database operations. Sessions
load instances from the database, track changes to mapped
instances and persist changes in a single unit of work when
flushed.
.. seealso::
:doc:`orm/session`
columns clause
The portion of the ``SELECT`` statement which enumerates the
SQL expressions to be returned in the result set. The expressions
follow the ``SELECT`` keyword directly and are a comma-separated
list of individual expressions.
E.g.:
.. sourcecode:: sql
SELECT user_account.name, user_account.email
FROM user_account WHERE user_account.name = 'fred'
Above, the list of columns ``user_acount.name``,
``user_account.email`` is the columns clause of the ``SELECT``.
WHERE clause
The portion of the ``SELECT`` statement which indicates criteria
by which rows should be filtered. It is a single SQL expression
which follows the keyword ``WHERE``.
.. sourcecode:: sql
SELECT user_account.name, user_account.email
FROM user_account
WHERE user_account.name = 'fred' AND user_account.status = 'E'
Above, the phrase ``WHERE user_account.name = 'fred' AND user_account.status = 'E'``
comprises the WHERE clause of the ``SELECT``.
FROM clause
The portion of the ``SELECT`` statement which indicates the initial
source of rows.
A simple ``SELECT`` will feature one or more table names in its
FROM clause. Multiple sources are separated by a comma:
.. sourcecode:: sql
SELECT user.name, address.email_address
FROM user, address
WHERE user.id=address.user_id
The FROM clause is also where explicit joins are specified. We can
rewrite the above ``SELECT`` using a single ``FROM`` element which consists
of a ``JOIN`` of the two tables:
.. sourcecode:: sql
SELECT user.name, address.email_address
FROM user JOIN address ON user.id=address.user_id
subquery
scalar subquery
Refers to a ``SELECT`` statement that is embedded within an enclosing
``SELECT``.
A subquery comes in two general flavors, one known as a "scalar select"
which specifically must return exactly one row and one column, and the
other form which acts as a "derived table" and serves as a source of
rows for the FROM clause of another select. A scalar select is eligible
to be placed in the :term:`WHERE clause`, :term:`columns clause`,
ORDER BY clause or HAVING clause of the enclosing select, whereas the
derived table form is eligible to be placed in the FROM clause of the
enclosing ``SELECT``.
Examples:
1. a scalar subquery placed in the :term:`columns clause` of an enclosing
``SELECT``. The subquery in this example is a :term:`correlated subquery` because part
of the rows which it selects from are given via the enclosing statement.
.. sourcecode:: sql
SELECT id, (SELECT name FROM address WHERE address.user_id=user.id)
FROM user
2. a scalar subquery placed in the :term:`WHERE clause` of an enclosing
``SELECT``. This subquery in this example is not correlated as it selects a fixed result.
.. sourcecode:: sql
SELECT id, name FROM user
WHERE status=(SELECT status_id FROM status_code WHERE code='C')
3. a derived table subquery placed in the :term:`FROM clause` of an enclosing
``SELECT``. Such a subquery is almost always given an alias name.
.. sourcecode:: sql
SELECT user.id, user.name, ad_subq.email_address
FROM
user JOIN
(select user_id, email_address FROM address WHERE address_type='Q') AS ad_subq
ON user.id = ad_subq.user_id
correlates
correlated subquery
correlated subqueries
A :term:`subquery` is correlated if it depends on data in the
enclosing ``SELECT``.
Below, a subquery selects the aggregate value ``MIN(a.id)``
from the ``email_address`` table, such that
it will be invoked for each value of ``user_account.id``, correlating
the value of this column against the ``email_address.user_account_id``
column:
.. sourcecode:: sql
SELECT user_account.name, email_address.email
FROM user_account
JOIN email_address ON user_account.id=email_address.user_account_id
WHERE email_address.id = (
SELECT MIN(a.id) FROM email_address AS a
WHERE a.user_account_id=user_account.id
)
The above subquery refers to the ``user_account`` table, which is not itself
in the ``FROM`` clause of this nested query. Instead, the ``user_account``
table is received from the enclosing query, where each row selected from
``user_account`` results in a distinct execution of the subquery.
A correlated subquery is in most cases present in the :term:`WHERE clause`
or :term:`columns clause` of the immediately enclosing ``SELECT``
statement, as well as in the ORDER BY or HAVING clause.
In less common cases, a correlated subquery may be present in the
:term:`FROM clause` of an enclosing ``SELECT``; in these cases the
correlation is typically due to the enclosing ``SELECT`` itself being
enclosed in the WHERE,
ORDER BY, columns or HAVING clause of another ``SELECT``, such as:
.. sourcecode:: sql
SELECT parent.id FROM parent
WHERE EXISTS (
SELECT * FROM (
SELECT child.id AS id, child.parent_id AS parent_id, child.pos AS pos
FROM child
WHERE child.parent_id = parent.id ORDER BY child.pos
LIMIT 3)
WHERE id = 7)
Correlation from one ``SELECT`` directly to one which encloses the correlated
query via its ``FROM``
clause is not possible, because the correlation can only proceed once the
original source rows from the enclosing statement's FROM clause are available.
ACID
ACID model
An acronym for "Atomicity, Consistency, Isolation,
Durability"; a set of properties that guarantee that
database transactions are processed reliably.
(via Wikipedia)
.. seealso::
:term:`atomicity`
:term:`consistency`
:term:`isolation`
:term:`durability`
`ACID Model (via Wikipedia) <https://en.wikipedia.org/wiki/ACID_Model>`_
atomicity
Atomicity is one of the components of the :term:`ACID` model,
and requires that each transaction is "all or nothing":
if one part of the transaction fails, the entire transaction
fails, and the database state is left unchanged. An atomic
system must guarantee atomicity in each and every situation,
including power failures, errors, and crashes.
(via Wikipedia)
.. seealso::
:term:`ACID`
`Atomicity (via Wikipedia) <https://en.wikipedia.org/wiki/Atomicity_(database_systems)>`_
consistency
Consistency is one of the components of the :term:`ACID` model,
and ensures that any transaction will
bring the database from one valid state to another. Any data
written to the database must be valid according to all defined
rules, including but not limited to :term:`constraints`, cascades,
triggers, and any combination thereof.
(via Wikipedia)
.. seealso::
:term:`ACID`
`Consistency (via Wikipedia) <https://en.wikipedia.org/wiki/Consistency_(database_systems)>`_
isolation
isolated
Isolation
isolation level
The isolation property of the :term:`ACID` model
ensures that the concurrent execution
of transactions results in a system state that would be
obtained if transactions were executed serially, i.e. one
after the other. Each transaction must execute in total
isolation i.e. if T1 and T2 execute concurrently then each
should remain independent of the other.
(via Wikipedia)
.. seealso::
:term:`ACID`
`Isolation (via Wikipedia) <https://en.wikipedia.org/wiki/Isolation_(database_systems)>`_
:term:`read uncommitted`
:term:`read committed`
:term:`repeatable read`
:term:`serializable`
repeatable read
One of the four database :term:`isolation` levels, repeatable read
features all of the isolation of :term:`read committed`, and
additionally features that any particular row that is read within a
transaction is guaranteed from that point to not have any subsequent
external changes in value (i.e. from other concurrent UPDATE
statements) for the duration of that transaction.
read committed
One of the four database :term:`isolation` levels, read committed
features that the transaction will not be exposed to any data from
other concurrent transactions that has not been committed yet,
preventing so-called "dirty reads". However, under read committed
there can be non-repeatable reads, meaning data in a row may change
when read a second time if another transaction has committed changes.
read uncommitted
One of the four database :term:`isolation` levels, read uncommitted
features that changes made to database data within a transaction will
not become permanent until the transaction is committed. However,
within read uncommitted, it may be possible for data that is not
committed in other transactions to be viewable within the scope of
another transaction; these are known as "dirty reads".
serializable
One of the four database :term:`isolation` levels, serializable
features all of the isolation of :term:`repeatable read`, and
additionally within a lock-based approach guarantees that so-called
"phantom reads" cannot occur; this means that rows which are INSERTed
or DELETEd within the scope of other transactions will not be
detectable within this transaction. A row that is read within this
transaction is guaranteed to continue existing, and a row that does not
exist is guaranteed that it cannot appear of inserted from another
transaction.
Serializable isolation typically relies upon locking of rows or ranges
of rows in order to achieve this effect and can increase the chance of
deadlocks and degrade performance. There are also non-lock based
schemes however these necessarily rely upon rejecting transactions if
write collisions are detected.
durability
Durability is a property of the :term:`ACID` model
which means that once a transaction has been committed,
it will remain so, even in the event of power loss, crashes,
or errors. In a relational database, for instance, once a
group of SQL statements execute, the results need to be stored
permanently (even if the database crashes immediately
thereafter).
(via Wikipedia)
.. seealso::
:term:`ACID`
`Durability (via Wikipedia) <https://en.wikipedia.org/wiki/Durability_(database_systems)>`_
RETURNING
This is a non-SQL standard clause provided in various forms by
certain backends, which provides the service of returning a result
set upon execution of an INSERT, UPDATE or DELETE statement. Any set
of columns from the matched rows can be returned, as though they were
produced from a SELECT statement.
The RETURNING clause provides both a dramatic performance boost to
common update/select scenarios, including retrieval of inline- or
default- generated primary key values and defaults at the moment they
were created, as well as a way to get at server-generated
default values in an atomic way.
An example of RETURNING, idiomatic to PostgreSQL, looks like:
.. sourcecode:: sql
INSERT INTO user_account (name) VALUES ('new name') RETURNING id, timestamp
Above, the INSERT statement will provide upon execution a result set
which includes the values of the columns ``user_account.id`` and
``user_account.timestamp``, which above should have been generated as default
values as they are not included otherwise (but note any series of columns
or SQL expressions can be placed into RETURNING, not just default-value columns).
The backends that currently support
RETURNING or a similar construct are PostgreSQL, SQL Server, Oracle,
and Firebird. The PostgreSQL and Firebird implementations are generally
full featured, whereas the implementations of SQL Server and Oracle
have caveats. On SQL Server, the clause is known as "OUTPUT INSERTED"
for INSERT and UPDATE statements and "OUTPUT DELETED" for DELETE statements;
the key caveat is that triggers are not supported in conjunction with this
keyword. On Oracle, it is known as "RETURNING...INTO", and requires that the
value be placed into an OUT parameter, meaning not only is the syntax awkward,
but it can also only be used for one row at a time.
SQLAlchemy's :meth:`.UpdateBase.returning` system provides a layer of abstraction
on top of the RETURNING systems of these backends to provide a consistent
interface for returning columns. The ORM also includes many optimizations
that make use of RETURNING when available.
one to many
A style of :func:`~sqlalchemy.orm.relationship` which links
the primary key of the parent mapper's table to the foreign
key of a related table. Each unique parent object can
then refer to zero or more unique related objects.
The related objects in turn will have an implicit or
explicit :term:`many to one` relationship to their parent
object.
An example one to many schema (which, note, is identical
to the :term:`many to one` schema):
.. sourcecode:: sql
CREATE TABLE department (
id INTEGER PRIMARY KEY,
name VARCHAR(30)
)
CREATE TABLE employee (
id INTEGER PRIMARY KEY,
name VARCHAR(30),
dep_id INTEGER REFERENCES department(id)
)
The relationship from ``department`` to ``employee`` is
one to many, since many employee records can be associated with a
single department. A SQLAlchemy mapping might look like::
class Department(Base):
__tablename__ = "department"
id = Column(Integer, primary_key=True)
name = Column(String(30))
employees = relationship("Employee")
class Employee(Base):
__tablename__ = "employee"
id = Column(Integer, primary_key=True)
name = Column(String(30))
dep_id = Column(Integer, ForeignKey("department.id"))
.. seealso::
:term:`relationship`
:term:`many to one`
:term:`backref`
many to one
A style of :func:`~sqlalchemy.orm.relationship` which links
a foreign key in the parent mapper's table to the primary
key of a related table. Each parent object can
then refer to exactly zero or one related object.
The related objects in turn will have an implicit or
explicit :term:`one to many` relationship to any number
of parent objects that refer to them.
An example many to one schema (which, note, is identical
to the :term:`one to many` schema):
.. sourcecode:: sql
CREATE TABLE department (
id INTEGER PRIMARY KEY,
name VARCHAR(30)
)
CREATE TABLE employee (
id INTEGER PRIMARY KEY,
name VARCHAR(30),
dep_id INTEGER REFERENCES department(id)
)
The relationship from ``employee`` to ``department`` is
many to one, since many employee records can be associated with a
single department. A SQLAlchemy mapping might look like::
class Department(Base):
__tablename__ = "department"
id = Column(Integer, primary_key=True)
name = Column(String(30))
class Employee(Base):
__tablename__ = "employee"
id = Column(Integer, primary_key=True)
name = Column(String(30))
dep_id = Column(Integer, ForeignKey("department.id"))
department = relationship("Department")
.. seealso::
:term:`relationship`
:term:`one to many`
:term:`backref`
backref
bidirectional relationship
An extension to the :term:`relationship` system whereby two
distinct :func:`~sqlalchemy.orm.relationship` objects can be
mutually associated with each other, such that they coordinate
in memory as changes occur to either side. The most common
way these two relationships are constructed is by using
the :func:`~sqlalchemy.orm.relationship` function explicitly
for one side and specifying the ``backref`` keyword to it so that
the other :func:`~sqlalchemy.orm.relationship` is created
automatically. We can illustrate this against the example we've
used in :term:`one to many` as follows::
class Department(Base):
__tablename__ = "department"
id = Column(Integer, primary_key=True)
name = Column(String(30))
employees = relationship("Employee", backref="department")
class Employee(Base):
__tablename__ = "employee"
id = Column(Integer, primary_key=True)
name = Column(String(30))
dep_id = Column(Integer, ForeignKey("department.id"))
A backref can be applied to any relationship, including one to many,
many to one, and :term:`many to many`.
.. seealso::
:term:`relationship`
:term:`one to many`
:term:`many to one`
:term:`many to many`
many to many
A style of :func:`sqlalchemy.orm.relationship` which links two tables together
via an intermediary table in the middle. Using this configuration,
any number of rows on the left side may refer to any number of
rows on the right, and vice versa.
A schema where employees can be associated with projects:
.. sourcecode:: sql
CREATE TABLE employee (
id INTEGER PRIMARY KEY,
name VARCHAR(30)
)
CREATE TABLE project (
id INTEGER PRIMARY KEY,
name VARCHAR(30)
)
CREATE TABLE employee_project (
employee_id INTEGER PRIMARY KEY,
project_id INTEGER PRIMARY KEY,
FOREIGN KEY employee_id REFERENCES employee(id),
FOREIGN KEY project_id REFERENCES project(id)
)
Above, the ``employee_project`` table is the many-to-many table,
which naturally forms a composite primary key consisting
of the primary key from each related table.
In SQLAlchemy, the :func:`sqlalchemy.orm.relationship` function
can represent this style of relationship in a mostly
transparent fashion, where the many-to-many table is
specified using plain table metadata::
class Employee(Base):
__tablename__ = "employee"
id = Column(Integer, primary_key=True)
name = Column(String(30))
projects = relationship(
"Project",
secondary=Table(
"employee_project",
Base.metadata,
Column("employee_id", Integer, ForeignKey("employee.id"), primary_key=True),
Column("project_id", Integer, ForeignKey("project.id"), primary_key=True),
),
backref="employees",
)
class Project(Base):
__tablename__ = "project"
id = Column(Integer, primary_key=True)
name = Column(String(30))
Above, the ``Employee.projects`` and back-referencing ``Project.employees``
collections are defined::
proj = Project(name="Client A")
emp1 = Employee(name="emp1")
emp2 = Employee(name="emp2")
proj.employees.extend([emp1, emp2])
.. seealso::
:term:`association relationship`
:term:`relationship`
:term:`one to many`
:term:`many to one`
relationship
relationships
A connecting unit between two mapped classes, corresponding
to some relationship between the two tables in the database.
The relationship is defined using the SQLAlchemy function
:func:`~sqlalchemy.orm.relationship`. Once created, SQLAlchemy
inspects the arguments and underlying mappings involved
in order to classify the relationship as one of three types:
:term:`one to many`, :term:`many to one`, or :term:`many to many`.
With this classification, the relationship construct
handles the task of persisting the appropriate linkages
in the database in response to in-memory object associations,
as well as the job of loading object references and collections
into memory based on the current linkages in the
database.
.. seealso::
:ref:`relationship_config_toplevel`
cursor
A control structure that enables traversal over the records in a database.
In the Python DBAPI, the cursor object is in fact the starting point
for statement execution as well as the interface used for fetching
results.
.. seealso::
`Cursor Objects (in pep-249) <https://www.python.org/dev/peps/pep-0249/#cursor-objects>`_
`Cursor (via Wikipedia) <https://en.wikipedia.org/wiki/Cursor_(databases)>`_
association relationship
A two-tiered :term:`relationship` which links two tables
together using an association table in the middle. The
association relationship differs from a :term:`many to many`
relationship in that the many-to-many table is mapped
by a full class, rather than invisibly handled by the
:func:`sqlalchemy.orm.relationship` construct as in the case
with many-to-many, so that additional attributes are
explicitly available.
For example, if we wanted to associate employees with
projects, also storing the specific role for that employee
with the project, the relational schema might look like:
.. sourcecode:: sql
CREATE TABLE employee (
id INTEGER PRIMARY KEY,
name VARCHAR(30)
)
CREATE TABLE project (
id INTEGER PRIMARY KEY,
name VARCHAR(30)
)
CREATE TABLE employee_project (
employee_id INTEGER PRIMARY KEY,
project_id INTEGER PRIMARY KEY,
role_name VARCHAR(30),
FOREIGN KEY employee_id REFERENCES employee(id),
FOREIGN KEY project_id REFERENCES project(id)
)
A SQLAlchemy declarative mapping for the above might look like::
class Employee(Base):
__tablename__ = "employee"
id = Column(Integer, primary_key=True)
name = Column(String(30))
class Project(Base):
__tablename__ = "project"
id = Column(Integer, primary_key=True)
name = Column(String(30))
class EmployeeProject(Base):
__tablename__ = "employee_project"
employee_id = Column(Integer, ForeignKey("employee.id"), primary_key=True)
project_id = Column(Integer, ForeignKey("project.id"), primary_key=True)
role_name = Column(String(30))
project = relationship("Project", backref="project_employees")
employee = relationship("Employee", backref="employee_projects")
Employees can be added to a project given a role name::
proj = Project(name="Client A")
emp1 = Employee(name="emp1")
emp2 = Employee(name="emp2")
proj.project_employees.extend(
[
EmployeeProject(employee=emp1, role_name="tech lead"),
EmployeeProject(employee=emp2, role_name="account executive"),
]
)
.. seealso::
:term:`many to many`
constraint
constraints
constrained
Rules established within a relational database that ensure
the validity and consistency of data. Common forms
of constraint include :term:`primary key constraint`,
:term:`foreign key constraint`, and :term:`check constraint`.
candidate key
A :term:`relational algebra` term referring to an attribute or set
of attributes that form a uniquely identifying key for a
row. A row may have more than one candidate key, each of which
is suitable for use as the primary key of that row.
The primary key of a table is always a candidate key.
.. seealso::
:term:`primary key`
`Candidate key (via Wikipedia) <https://en.wikipedia.org/wiki/Candidate_key>`_
https://www.databasestar.com/database-keys/
primary key
primary key constraint
A :term:`constraint` that uniquely defines the characteristics
of each row in a table. The primary key has to consist of
characteristics that cannot be duplicated by any other row.
The primary key may consist of a single attribute or
multiple attributes in combination.
(via Wikipedia)
The primary key of a table is typically, though not always,
defined within the ``CREATE TABLE`` :term:`DDL`:
.. sourcecode:: sql
CREATE TABLE employee (
emp_id INTEGER,
emp_name VARCHAR(30),
dep_id INTEGER,
PRIMARY KEY (emp_id)
)
.. seealso::
:term:`composite primary key`
`Primary key (via Wikipedia) <https://en.wikipedia.org/wiki/Primary_Key>`_
composite primary key
A :term:`primary key` that has more than one column. A particular
database row is unique based on two or more columns rather than just
a single value.
.. seealso::
:term:`primary key`
foreign key constraint
A referential constraint between two tables. A foreign key is a field or set of fields in a
relational table that matches a :term:`candidate key` of another table.
The foreign key can be used to cross-reference tables.
(via Wikipedia)
A foreign key constraint can be added to a table in standard
SQL using :term:`DDL` like the following:
.. sourcecode:: sql
ALTER TABLE employee ADD CONSTRAINT dep_id_fk
FOREIGN KEY (employee) REFERENCES department (dep_id)
.. seealso::
`Foreign Key Constraint (via Wikipedia) <https://en.wikipedia.org/wiki/Foreign_key_constraint>`_
check constraint
A check constraint is a
condition that defines valid data when adding or updating an
entry in a table of a relational database. A check constraint
is applied to each row in the table.
(via Wikipedia)
A check constraint can be added to a table in standard
SQL using :term:`DDL` like the following:
.. sourcecode:: sql
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
.. seealso::
`CHECK constraint (via Wikipedia) <https://en.wikipedia.org/wiki/Check_constraint>`_
unique constraint
unique key index
A unique key index can uniquely identify each row of data
values in a database table. A unique key index comprises a
single column or a set of columns in a single database table.
No two distinct rows or data records in a database table can
have the same data value (or combination of data values) in
those unique key index columns if NULL values are not used.
Depending on its design, a database table may have many unique
key indexes but at most one primary key index.
(via Wikipedia)
.. seealso::
`Unique key (via Wikipedia) <https://en.wikipedia.org/wiki/Unique_key#Defining_unique_keys>`_
transient
This describes one of the major object states which
an object can have within a :term:`Session`; a transient object
is a new object that doesn't have any database identity
and has not been associated with a session yet. When the
object is added to the session, it moves to the
:term:`pending` state.
.. seealso::
:ref:`session_object_states`
pending
This describes one of the major object states which
an object can have within a :term:`Session`; a pending object
is a new object that doesn't have any database identity,
but has been recently associated with a session. When
the session emits a flush and the row is inserted, the
object moves to the :term:`persistent` state.
.. seealso::
:ref:`session_object_states`
deleted
This describes one of the major object states which
an object can have within a :term:`Session`; a deleted object
is an object that was formerly persistent and has had a
DELETE statement emitted to the database within a flush
to delete its row. The object will move to the :term:`detached`
state once the session's transaction is committed; alternatively,
if the session's transaction is rolled back, the DELETE is
reverted and the object moves back to the :term:`persistent`
state.
.. seealso::
:ref:`session_object_states`
persistent
This describes one of the major object states which
an object can have within a :term:`Session`; a persistent object
is an object that has a database identity (i.e. a primary key)
and is currently associated with a session. Any object
that was previously :term:`pending` and has now been inserted
is in the persistent state, as is any object that's
been loaded by the session from the database. When a
persistent object is removed from a session, it is known
as :term:`detached`.
.. seealso::
:ref:`session_object_states`
detached
This describes one of the major object states which
an object can have within a :term:`Session`; a detached object
is an object that has a database identity (i.e. a primary key)
but is not associated with any session. An object that
was previously :term:`persistent` and was removed from its
session either because it was expunged, or the owning
session was closed, moves into the detached state.
The detached state is generally used when objects are being
moved between sessions or when being moved to/from an external
object cache.
.. seealso::
:ref:`session_object_states`
attached
Indicates an ORM object that is presently associated with a specific
:term:`Session`.
.. seealso::
:ref:`session_object_states`
|