sqlobjects 2.0.0__py3-none-any.whl → 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlobjects/__init__.py +1 -1
- sqlobjects/cascade.py +22 -31
- sqlobjects/contrib/asgi.py +5 -3
- sqlobjects/database/manager.py +44 -30
- sqlobjects/expressions/aggregate.py +144 -83
- sqlobjects/expressions/scalar.py +5 -9
- sqlobjects/fields/__init__.py +2 -0
- sqlobjects/fields/core.py +3 -2
- sqlobjects/fields/proxies.py +78 -30
- sqlobjects/fields/relations/descriptors.py +4 -2
- sqlobjects/fields/relations/prefetch.py +62 -63
- sqlobjects/fields/relations/utils.py +50 -11
- sqlobjects/internal/results.py +1 -1
- sqlobjects/metadata.py +124 -29
- sqlobjects/mixins.py +24 -5
- sqlobjects/model.py +45 -72
- sqlobjects/objects/bulk.py +159 -245
- sqlobjects/objects/core.py +62 -58
- sqlobjects/objects/upsert.py +85 -8
- sqlobjects/queries/builder.py +77 -145
- sqlobjects/queries/dialect.py +4 -4
- sqlobjects/queries/executor.py +105 -209
- sqlobjects/queryset.py +64 -45
- sqlobjects/session.py +342 -38
- sqlobjects/signals.py +60 -33
- sqlobjects/sql_logging.py +100 -11
- sqlobjects/utils/naming.py +17 -1
- {sqlobjects-2.0.0.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/01-database-session-guide.md +44 -5
- {sqlobjects-2.0.0.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/02-model-definition-guide.md +25 -0
- {sqlobjects-2.0.0.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/03-query-operations-guide.md +33 -4
- {sqlobjects-2.0.0.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/05-relationships-guide.md +37 -15
- {sqlobjects-2.0.0.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/06-validation-signals-guide.md +32 -0
- {sqlobjects-2.0.0.dist-info → sqlobjects-2.1.0.dist-info}/METADATA +9 -2
- sqlobjects-2.1.0.dist-info/RECORD +75 -0
- sqlobjects-2.0.0.dist-info/RECORD +0 -75
- {sqlobjects-2.0.0.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/04-crud-operations-guide.md +0 -0
- {sqlobjects-2.0.0.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/07-performance-guide.md +0 -0
- {sqlobjects-2.0.0.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/README.md +0 -0
- {sqlobjects-2.0.0.dist-info → sqlobjects-2.1.0.dist-info}/WHEEL +0 -0
- {sqlobjects-2.0.0.dist-info → sqlobjects-2.1.0.dist-info}/entry_points.txt +0 -0
- {sqlobjects-2.0.0.dist-info → sqlobjects-2.1.0.dist-info}/licenses/LICENSE +0 -0
- {sqlobjects-2.0.0.dist-info → sqlobjects-2.1.0.dist-info}/top_level.txt +0 -0
sqlobjects/fields/proxies.py
CHANGED
|
@@ -15,12 +15,13 @@ from typing import TYPE_CHECKING, Any, Generic, TypeVar
|
|
|
15
15
|
|
|
16
16
|
from sqlalchemy import join, select
|
|
17
17
|
|
|
18
|
-
from ..exceptions import DeferredFieldError
|
|
18
|
+
from ..exceptions import DeferredFieldError, PrimaryKeyError, QueryError
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
if TYPE_CHECKING:
|
|
22
22
|
from ..mixins import DeferredLoadingMixin
|
|
23
23
|
from ..model import ObjectModel
|
|
24
|
+
from ..queryset import QuerySet
|
|
24
25
|
from .relations.descriptors import RelationshipDescriptor
|
|
25
26
|
|
|
26
27
|
|
|
@@ -379,40 +380,87 @@ class ManyToManyRelation(RelatedCollection[T]):
|
|
|
379
380
|
return f"ManyToManyRelation(field='{self.property.name}')"
|
|
380
381
|
|
|
381
382
|
|
|
382
|
-
class RelatedQuerySet(BaseRelated[T]):
|
|
383
|
-
"""
|
|
383
|
+
class RelatedQuerySet(BaseRelated[list[T]]):
|
|
384
|
+
"""Build fresh, parent-scoped queries for lazy='dynamic' collections."""
|
|
384
385
|
|
|
385
|
-
def
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
def _get_queryset(self) -> Any:
|
|
393
|
-
"""Lazy initialize QuerySet."""
|
|
394
|
-
if not self._initialized:
|
|
395
|
-
# QuerySet will be implemented in Layer 5
|
|
396
|
-
self._queryset = None
|
|
397
|
-
self._initialized = True
|
|
398
|
-
return self._queryset
|
|
386
|
+
def _reference_value(self, field: str) -> Any:
|
|
387
|
+
value = getattr(self.instance, field)
|
|
388
|
+
if isinstance(value, DeferredObject):
|
|
389
|
+
raise DeferredFieldError(
|
|
390
|
+
f"Load relationship key '{field}' before querying dynamic relationship '{self.property.name}'"
|
|
391
|
+
)
|
|
392
|
+
return value
|
|
399
393
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
self._cached_value = self._get_queryset()
|
|
403
|
-
self._loaded = True
|
|
394
|
+
def _get_queryset(self) -> "QuerySet[T]":
|
|
395
|
+
from ..queryset import QuerySet
|
|
404
396
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
397
|
+
if self.property.primaryjoin is not None or self.property.secondaryjoin is not None:
|
|
398
|
+
raise QueryError("Dynamic relationships do not support custom join conditions")
|
|
399
|
+
info = self._get_relationship_info()
|
|
400
|
+
if not info or info["type"] not in {"reverse_fk", "many_to_many"}:
|
|
401
|
+
raise QueryError(f"Dynamic relationship '{self.property.name}' requires a resolved collection relationship")
|
|
402
|
+
if not self.instance._has_primary_key_values():
|
|
403
|
+
raise PrimaryKeyError("A dynamic relationship requires a parent with complete primary key values")
|
|
404
|
+
|
|
405
|
+
model = info["related_model"]
|
|
406
|
+
table = model.get_table()
|
|
407
|
+
queryset = QuerySet(table, model, self.instance._state_manager.get_bound_session())
|
|
408
|
+
ordering = self.property.order_by
|
|
409
|
+
if ordering is not None:
|
|
410
|
+
queryset = queryset.order_by(*([ordering] if isinstance(ordering, str) else ordering))
|
|
411
|
+
|
|
412
|
+
if info["type"] == "many_to_many":
|
|
413
|
+
value = self._reference_value(info["left_ref_field"])
|
|
414
|
+
if value is None:
|
|
415
|
+
return queryset.none()
|
|
416
|
+
through = self.instance.__class__.__registry__.tables[info["through_table"]]
|
|
417
|
+
# Membership avoids duplicate target rows, even if the association
|
|
418
|
+
# table permits repeated links to the same target.
|
|
419
|
+
members = select(through.c[info["right_field"]]).where(through.c[info["left_field"]] == value)
|
|
420
|
+
return queryset.filter(table.c[info["right_ref_field"]].in_(members))
|
|
421
|
+
|
|
422
|
+
conditions = []
|
|
423
|
+
for foreign_key, reference in zip(info["foreign_key_fields"], info["ref_fields"], strict=True):
|
|
424
|
+
value = self._reference_value(reference)
|
|
425
|
+
if value is None:
|
|
426
|
+
return queryset.none()
|
|
427
|
+
conditions.append(table.c[foreign_key] == value)
|
|
428
|
+
if not conditions:
|
|
429
|
+
raise QueryError(f"Dynamic relationship '{self.property.name}' has no foreign key mapping")
|
|
430
|
+
return queryset.filter(*conditions)
|
|
431
|
+
|
|
432
|
+
async def fetch(self) -> list[T]:
|
|
433
|
+
"""Read the current related rows without caching a materialized list."""
|
|
434
|
+
return await self._get_queryset().all()
|
|
409
435
|
|
|
410
436
|
def __getattr__(self, name: str) -> Any:
|
|
411
|
-
"""Proxy
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
437
|
+
"""Proxy public QuerySet methods.
|
|
438
|
+
|
|
439
|
+
Unknown and private names raise AttributeError so ``hasattr()``,
|
|
440
|
+
``getattr(obj, name, default)``, ``copy``/``pickle`` protocol lookups and
|
|
441
|
+
``to_dict(safe_access=True)`` keep working; only real QuerySet methods
|
|
442
|
+
build a parent-scoped query, which requires a saved parent.
|
|
443
|
+
"""
|
|
444
|
+
from ..queryset import QuerySet
|
|
445
|
+
|
|
446
|
+
if name.startswith("_") or not hasattr(QuerySet, name):
|
|
447
|
+
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
|
|
448
|
+
return getattr(self._get_queryset(), name)
|
|
449
|
+
|
|
450
|
+
def __getitem__(self, key) -> "QuerySet[T]":
|
|
451
|
+
return self._get_queryset()[key]
|
|
452
|
+
|
|
453
|
+
def __aiter__(self):
|
|
454
|
+
return self._get_queryset().iterator()
|
|
455
|
+
|
|
456
|
+
def __iter__(self):
|
|
457
|
+
raise TypeError("Use async iteration or await relation.fetch() for a dynamic relationship")
|
|
458
|
+
|
|
459
|
+
def __len__(self):
|
|
460
|
+
raise TypeError("Use await relation.count() for a dynamic relationship")
|
|
461
|
+
|
|
462
|
+
def __bool__(self):
|
|
463
|
+
raise TypeError("Use await relation.exists() for a dynamic relationship")
|
|
416
464
|
|
|
417
465
|
def __str__(self):
|
|
418
466
|
return f"<RelatedQuerySet: {self.property.name}>"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import TYPE_CHECKING, Generic, TypeVar, overload
|
|
1
|
+
from typing import TYPE_CHECKING, Generic, TypeVar, cast, overload
|
|
2
2
|
|
|
3
3
|
from ...cascade import OnDelete
|
|
4
4
|
|
|
@@ -144,7 +144,9 @@ class RelationshipDescriptor(Generic[T]):
|
|
|
144
144
|
)
|
|
145
145
|
|
|
146
146
|
if self.property.lazy == "dynamic":
|
|
147
|
-
|
|
147
|
+
# T describes the collection here; RelatedQuerySet's parameter is
|
|
148
|
+
# its element type, determined by the resolved relationship.
|
|
149
|
+
return cast("BaseRelated[T]", RelatedQuerySet(instance, self))
|
|
148
150
|
elif self.property.lazy == "noload":
|
|
149
151
|
return NoLoadRelation(instance, self)
|
|
150
152
|
elif self.property.lazy == "raise":
|
|
@@ -9,54 +9,45 @@ class PrefetchHandler:
|
|
|
9
9
|
def __init__(self, session):
|
|
10
10
|
self.session = session
|
|
11
11
|
|
|
12
|
-
async def handle_prefetch_relationships(self, instances, prefetch_relationships):
|
|
13
|
-
if not instances
|
|
12
|
+
async def handle_prefetch_relationships(self, instances, prefetch_relationships, configs=None):
|
|
13
|
+
if not instances:
|
|
14
14
|
return instances
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
for path in sorted(prefetch_relationships):
|
|
16
|
+
name, _, remainder = path.partition("__")
|
|
17
|
+
info = RelationshipAnalyzer.analyze_relationship(instances[0].__class__, name)
|
|
18
|
+
if not info:
|
|
19
|
+
raise ValueError(f"Invalid relationship '{name}'")
|
|
20
|
+
custom = (configs or {}).get(path) if not remainder else None
|
|
21
|
+
await self._prefetch_single_relationship(instances, name, info, custom)
|
|
22
|
+
if remainder:
|
|
23
|
+
related = []
|
|
24
|
+
for instance in instances:
|
|
25
|
+
value = instance._state_manager.get_object_cache().get(name)
|
|
26
|
+
related.extend(value if isinstance(value, list) else [value] if value is not None else [])
|
|
27
|
+
await self.handle_prefetch_relationships(
|
|
28
|
+
related, {remainder}, {remainder: configs[path]} if configs and path in configs else None
|
|
29
|
+
)
|
|
21
30
|
return instances
|
|
22
31
|
|
|
23
|
-
async def _prefetch_single_relationship(self, instances, relationship_name, relationship_info):
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
relationship_name,
|
|
40
|
-
info["related_model"],
|
|
41
|
-
info["ref_fields"],
|
|
42
|
-
info["foreign_key_fields"],
|
|
43
|
-
None,
|
|
44
|
-
)
|
|
45
|
-
elif rel_type == "many_to_one":
|
|
46
|
-
info = relationship_info
|
|
47
|
-
await self._prefetch_by_fields(
|
|
48
|
-
instances,
|
|
49
|
-
relationship_name,
|
|
50
|
-
info["related_model"],
|
|
51
|
-
info["foreign_key_fields"],
|
|
52
|
-
info["ref_fields"],
|
|
53
|
-
None,
|
|
54
|
-
)
|
|
55
|
-
elif rel_type == "many_to_many":
|
|
56
|
-
await self._prefetch_many_to_many(instances, relationship_name, relationship_info)
|
|
32
|
+
async def _prefetch_single_relationship(self, instances, relationship_name, relationship_info, queryset=None):
|
|
33
|
+
info = relationship_info
|
|
34
|
+
if queryset is not None and queryset._model_class is not info["related_model"]:
|
|
35
|
+
raise ValueError(f"Prefetch queryset model does not match relationship '{relationship_name}'")
|
|
36
|
+
if info["type"] == "many_to_many":
|
|
37
|
+
await self._prefetch_many_to_many(instances, relationship_name, info, queryset)
|
|
38
|
+
return
|
|
39
|
+
local, remote = (
|
|
40
|
+
(info["foreign_key_fields"], info["ref_fields"])
|
|
41
|
+
if info["type"] == "many_to_one"
|
|
42
|
+
else (info["ref_fields"], info["foreign_key_fields"])
|
|
43
|
+
)
|
|
44
|
+
empty = [] if info["type"] == "reverse_fk" else None
|
|
45
|
+
await self._prefetch_by_fields(
|
|
46
|
+
instances, relationship_name, info["related_model"], local, remote, empty, queryset
|
|
47
|
+
)
|
|
57
48
|
|
|
58
49
|
async def _prefetch_by_fields(
|
|
59
|
-
self, instances, relationship_name, related_model, lookup_fields, group_fields, empty
|
|
50
|
+
self, instances, relationship_name, related_model, lookup_fields, group_fields, empty, queryset=None
|
|
60
51
|
):
|
|
61
52
|
"""Unified prefetch for FK-based relationships."""
|
|
62
53
|
composite = len(lookup_fields) > 1
|
|
@@ -74,13 +65,21 @@ class PrefetchHandler:
|
|
|
74
65
|
inst._update_cache(relationship_name, [] if isinstance(empty, list) else empty)
|
|
75
66
|
return
|
|
76
67
|
|
|
68
|
+
qs = related_model.objects.using(self.session) if queryset is None else queryset
|
|
69
|
+
if queryset is not None and queryset._db_or_session is None:
|
|
70
|
+
qs = queryset.using(self.session)
|
|
71
|
+
if queryset is not None and (
|
|
72
|
+
queryset._builder.limits is not None or queryset._builder.offset_value is not None
|
|
73
|
+
):
|
|
74
|
+
raise ValueError(
|
|
75
|
+
"Sliced prefetch querysets are not supported; a global limit would truncate related collections"
|
|
76
|
+
)
|
|
77
|
+
if queryset is not None and queryset._builder.selected_fields:
|
|
78
|
+
qs = qs.only(*group_fields)
|
|
77
79
|
if composite:
|
|
78
|
-
|
|
79
|
-
qs = related_model.objects.using(self.session).filter(tuple_(*cols).in_(lookup_values))
|
|
80
|
+
qs = qs.filter(tuple_(*(getattr(related_model, f) for f in group_fields)).in_(lookup_values))
|
|
80
81
|
else:
|
|
81
|
-
qs =
|
|
82
|
-
getattr(related_model, group_fields[0]).in_(lookup_values)
|
|
83
|
-
)
|
|
82
|
+
qs = qs.filter(getattr(related_model, group_fields[0]).in_(lookup_values))
|
|
84
83
|
related_objects = await qs.all()
|
|
85
84
|
|
|
86
85
|
if isinstance(empty, list):
|
|
@@ -94,7 +93,7 @@ class PrefetchHandler:
|
|
|
94
93
|
for inst in instances:
|
|
95
94
|
inst._update_cache(relationship_name, related_map.get(make_key(inst, lookup_fields)))
|
|
96
95
|
|
|
97
|
-
async def _prefetch_many_to_many(self, instances, relationship_name, relationship_info):
|
|
96
|
+
async def _prefetch_many_to_many(self, instances, relationship_name, relationship_info, queryset=None):
|
|
98
97
|
"""Prefetch many-to-many via through table."""
|
|
99
98
|
related_model = relationship_info["related_model"]
|
|
100
99
|
through_table = relationship_info["through_table"]
|
|
@@ -111,27 +110,27 @@ class PrefetchHandler:
|
|
|
111
110
|
inst._update_cache(relationship_name, [])
|
|
112
111
|
return
|
|
113
112
|
|
|
114
|
-
|
|
115
|
-
if not through_model:
|
|
116
|
-
return
|
|
113
|
+
from sqlalchemy import select
|
|
117
114
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
.
|
|
121
|
-
.all()
|
|
115
|
+
table = instances[0].__class__.__registry__.tables[through_table]
|
|
116
|
+
result = await self.session.execute(
|
|
117
|
+
select(table.c[left_field], table.c[right_field]).where(table.c[left_field].in_(instance_values))
|
|
122
118
|
)
|
|
123
|
-
|
|
119
|
+
through_objects = result.fetchall()
|
|
124
120
|
related_values = [getattr(obj, right_field) for obj in through_objects]
|
|
125
121
|
if not related_values:
|
|
126
122
|
for inst in instances:
|
|
127
123
|
inst._update_cache(relationship_name, [])
|
|
128
124
|
return
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
.
|
|
134
|
-
|
|
125
|
+
qs = related_model.objects.using(self.session) if queryset is None else queryset
|
|
126
|
+
if queryset is not None:
|
|
127
|
+
if queryset._builder.limits is not None or queryset._builder.offset_value is not None:
|
|
128
|
+
raise ValueError("Sliced prefetch querysets are not supported")
|
|
129
|
+
if queryset._db_or_session is None:
|
|
130
|
+
qs = qs.using(self.session)
|
|
131
|
+
if queryset._builder.selected_fields:
|
|
132
|
+
qs = qs.only(right_ref_field)
|
|
133
|
+
related_objects = await qs.filter(getattr(related_model, right_ref_field).in_(related_values)).all()
|
|
135
134
|
|
|
136
135
|
related_map = {getattr(obj, right_ref_field): obj for obj in related_objects}
|
|
137
136
|
|
|
@@ -172,18 +172,31 @@ class RelationshipAnalyzer:
|
|
|
172
172
|
@staticmethod
|
|
173
173
|
@lru_cache(maxsize=256)
|
|
174
174
|
def analyze_relationship(model_class, relationship_name):
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
175
|
+
if hasattr(model_class, relationship_name):
|
|
176
|
+
field_attr = getattr(model_class, relationship_name)
|
|
177
|
+
if hasattr(field_attr, "property"):
|
|
178
|
+
return RelationshipAnalyzer._extract_relationship_info(model_class, field_attr.property)
|
|
179
|
+
# Preserve the conventional author_id -> author shortcut, using its actual
|
|
180
|
+
# foreign-key target rather than guessing a model or referenced column.
|
|
181
|
+
table = model_class.get_table()
|
|
182
|
+
column = table.c.get(f"{relationship_name}_id")
|
|
183
|
+
if column is not None and len(column.foreign_keys) == 1:
|
|
184
|
+
foreign_key = next(iter(column.foreign_keys))
|
|
185
|
+
related_model = model_class.__registry__.get_model_by_table(foreign_key.column.table.name)
|
|
186
|
+
if related_model is not None:
|
|
187
|
+
return {
|
|
188
|
+
"type": "many_to_one",
|
|
189
|
+
"related_model": related_model,
|
|
190
|
+
"foreign_key_fields": [column.name],
|
|
191
|
+
"ref_fields": [foreign_key.column.name],
|
|
192
|
+
}
|
|
193
|
+
return None
|
|
183
194
|
|
|
184
195
|
@staticmethod
|
|
185
196
|
def _extract_relationship_info(model_class, prop):
|
|
186
|
-
related_model =
|
|
197
|
+
related_model = (
|
|
198
|
+
model_class.__registry__.get_model(prop.argument) if isinstance(prop.argument, str) else prop.argument
|
|
199
|
+
)
|
|
187
200
|
if not related_model:
|
|
188
201
|
return None
|
|
189
202
|
|
|
@@ -191,7 +204,31 @@ class RelationshipAnalyzer:
|
|
|
191
204
|
if prop.secondary:
|
|
192
205
|
m2m_def = getattr(prop, "m2m_definition", None)
|
|
193
206
|
if not m2m_def:
|
|
194
|
-
|
|
207
|
+
through = model_class.__registry__.tables.get(prop.secondary)
|
|
208
|
+
if through is None:
|
|
209
|
+
raise ValueError(f"Unknown association table '{prop.secondary}'")
|
|
210
|
+
|
|
211
|
+
def referencing(table):
|
|
212
|
+
return [
|
|
213
|
+
(col.name, fk.column.name)
|
|
214
|
+
for col in through.c
|
|
215
|
+
for fk in col.foreign_keys
|
|
216
|
+
if fk.column.table is table
|
|
217
|
+
]
|
|
218
|
+
|
|
219
|
+
left = referencing(model_class.get_table())
|
|
220
|
+
right = referencing(related_model.get_table())
|
|
221
|
+
if len(left) != 1 or len(right) != 1:
|
|
222
|
+
raise ValueError("Ambiguous association table; supply an explicit M2MTable definition")
|
|
223
|
+
m2m_def = M2MTable(
|
|
224
|
+
prop.secondary,
|
|
225
|
+
model_class.__name__,
|
|
226
|
+
related_model.__name__,
|
|
227
|
+
left[0][0],
|
|
228
|
+
right[0][0],
|
|
229
|
+
left[0][1],
|
|
230
|
+
right[0][1],
|
|
231
|
+
)
|
|
195
232
|
return {
|
|
196
233
|
"type": "many_to_many",
|
|
197
234
|
"related_model": related_model,
|
|
@@ -314,7 +351,9 @@ class RelationshipAnalyzer:
|
|
|
314
351
|
for subclass in ObjectModel.__subclasses__():
|
|
315
352
|
if hasattr(subclass, "__registry__"):
|
|
316
353
|
try:
|
|
317
|
-
|
|
354
|
+
model = subclass.__registry__.get_model(argument)
|
|
355
|
+
if model is not None:
|
|
356
|
+
return model
|
|
318
357
|
except Exception:
|
|
319
358
|
continue
|
|
320
359
|
|
sqlobjects/internal/results.py
CHANGED
sqlobjects/metadata.py
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import re
|
|
2
|
+
from collections import Counter
|
|
2
3
|
from dataclasses import dataclass, field
|
|
3
4
|
from typing import TYPE_CHECKING, Any, Union, cast
|
|
4
5
|
|
|
5
6
|
from sqlalchemy import CheckConstraint, ForeignKeyConstraint, Index, UniqueConstraint
|
|
6
7
|
from sqlalchemy import MetaData as SqlAlchemyMetaData
|
|
8
|
+
from sqlalchemy.exc import CompileError
|
|
9
|
+
from sqlalchemy.sql.elements import conv
|
|
7
10
|
|
|
8
11
|
from .fields import ColumnAttribute
|
|
9
12
|
from .fields.relations import M2MTable, RelationshipDescriptor, RelationshipResolver
|
|
10
13
|
from .fields.utils import get_column_from_field, is_field_definition
|
|
11
|
-
from .utils.naming import to_snake_case
|
|
14
|
+
from .utils.naming import schema_name, to_snake_case
|
|
12
15
|
from .utils.pattern import pluralize
|
|
13
16
|
|
|
14
17
|
|
|
@@ -29,6 +32,23 @@ _FIELD_NAME_PATTERN = re.compile(r"\b([a-zA-Z_][a-zA-Z0-9_]*)\b")
|
|
|
29
32
|
_TEMP_INDEX_PREFIX = "__temp__idx_"
|
|
30
33
|
|
|
31
34
|
|
|
35
|
+
def _assign_schema_names(generated: list[tuple[Any, str, str]], explicit_names: set[str]) -> None:
|
|
36
|
+
"""Name generated objects without changing caller-supplied names."""
|
|
37
|
+
counts = Counter(schema_name(name) for _, name, _ in generated)
|
|
38
|
+
used = explicit_names | set(counts)
|
|
39
|
+
for item, full_name, signature in sorted(generated, key=lambda entry: (entry[1], entry[2])):
|
|
40
|
+
name = schema_name(full_name)
|
|
41
|
+
if counts[name] > 1 or name in explicit_names:
|
|
42
|
+
attempt = 0
|
|
43
|
+
while True:
|
|
44
|
+
name = schema_name(full_name, disambiguator=f"{signature}:{attempt}")
|
|
45
|
+
if name not in used:
|
|
46
|
+
break
|
|
47
|
+
attempt += 1
|
|
48
|
+
item.name = conv(name)
|
|
49
|
+
used.add(name)
|
|
50
|
+
|
|
51
|
+
|
|
32
52
|
@dataclass
|
|
33
53
|
class _RawModelConfig:
|
|
34
54
|
"""Raw model configuration with optional fields for parsing phase."""
|
|
@@ -314,7 +334,11 @@ class ModelProcessor(type):
|
|
|
314
334
|
# Check if this directly inherits from ObjectModel
|
|
315
335
|
direct_objectmodel_bases = [base for base in bases if base.__name__ == "ObjectModel"]
|
|
316
336
|
|
|
317
|
-
if
|
|
337
|
+
if "__registry__" in namespace:
|
|
338
|
+
registry = namespace["__registry__"]
|
|
339
|
+
if not isinstance(registry, ModelRegistry):
|
|
340
|
+
raise TypeError("__registry__ must be a ModelRegistry instance")
|
|
341
|
+
elif direct_objectmodel_bases:
|
|
318
342
|
# Direct ObjectModel inheritance - use ObjectModel's shared registry
|
|
319
343
|
objectmodel_base = direct_objectmodel_bases[0]
|
|
320
344
|
if not hasattr(objectmodel_base, "__registry__"):
|
|
@@ -448,11 +472,9 @@ class ModelProcessor(type):
|
|
|
448
472
|
|
|
449
473
|
# Collect indexes
|
|
450
474
|
if getattr(column, "unique", False) and not getattr(column, "primary_key", False):
|
|
451
|
-
|
|
452
|
-
indexes.append(Index(index_name, name, unique=True))
|
|
475
|
+
indexes.append(index(None, name, unique=True))
|
|
453
476
|
elif getattr(column, "index", False) and not getattr(column, "primary_key", False):
|
|
454
|
-
|
|
455
|
-
indexes.append(Index(index_name, name))
|
|
477
|
+
indexes.append(index(None, name))
|
|
456
478
|
|
|
457
479
|
# Collect validators
|
|
458
480
|
column_info = getattr(column, "info", None)
|
|
@@ -592,40 +614,104 @@ class ModelProcessor(type):
|
|
|
592
614
|
|
|
593
615
|
@classmethod
|
|
594
616
|
def _post_process_table_indexes(mcs, table, table_name: str) -> None:
|
|
595
|
-
def
|
|
596
|
-
|
|
617
|
+
def expression_sig(idx):
|
|
618
|
+
# Index.columns loses expression structure and repeated columns.
|
|
619
|
+
# Preserve expression order, sort direction, collation and literals.
|
|
620
|
+
# Expression keys also select PostgreSQL operator classes.
|
|
621
|
+
return tuple(
|
|
622
|
+
(
|
|
623
|
+
str(expr.compile(compile_kwargs={"literal_binds": True, "include_table": False})),
|
|
624
|
+
getattr(expr, "key", None),
|
|
625
|
+
)
|
|
626
|
+
for expr in idx.expressions
|
|
627
|
+
)
|
|
628
|
+
|
|
629
|
+
def dialect_sig(idx):
|
|
630
|
+
# Dialect kwargs (postgresql_where / postgresql_using / mysql_using, etc.)
|
|
631
|
+
# are part of an index's identity: same columns with different partial
|
|
632
|
+
# predicates or access methods are distinct indexes, not duplicates.
|
|
633
|
+
# Include literal values when comparing SQLAlchemy predicates.
|
|
634
|
+
# dialect_kwargs only holds explicitly passed args, not dialect defaults.
|
|
635
|
+
kwargs = getattr(idx, "dialect_kwargs", None) or {}
|
|
636
|
+
|
|
637
|
+
def render(value):
|
|
638
|
+
if not hasattr(value, "compile"):
|
|
639
|
+
return str(value)
|
|
640
|
+
try:
|
|
641
|
+
return str(value.compile(compile_kwargs={"literal_binds": True}))
|
|
642
|
+
except CompileError:
|
|
643
|
+
# Literal values of some types (JSON, custom types) have no
|
|
644
|
+
# generic renderer. Treat the predicate as unique rather than
|
|
645
|
+
# failing model definition or proving a false equivalence.
|
|
646
|
+
return f"<unrenderable {type(value).__name__} {id(value):#x}>"
|
|
647
|
+
|
|
648
|
+
return tuple(sorted((k, render(v)) for k, v in kwargs.items() if v is not None))
|
|
649
|
+
|
|
650
|
+
def is_partial(idx):
|
|
651
|
+
return any(k.endswith("_where") and value is not None for k, value in idx.dialect_kwargs.items())
|
|
597
652
|
|
|
598
|
-
full_sig_map: dict[tuple, list] = {} # (
|
|
599
|
-
|
|
653
|
+
full_sig_map: dict[tuple, list] = {} # (expressions, unique, options) -> indexes
|
|
654
|
+
shape_map: dict[tuple, list] = {} # (expressions, options) -> indexes
|
|
600
655
|
|
|
601
656
|
for idx in list(table.indexes):
|
|
602
|
-
|
|
603
|
-
|
|
657
|
+
# Conditional DDL can produce different schemas per dialect or
|
|
658
|
+
# callback result. Keep it intact instead of evaluating callbacks.
|
|
659
|
+
if getattr(idx, "_ddl_if", None) is not None:
|
|
604
660
|
continue
|
|
605
|
-
|
|
606
|
-
|
|
661
|
+
try:
|
|
662
|
+
expressions, options = expression_sig(idx), dialect_sig(idx)
|
|
663
|
+
except CompileError:
|
|
664
|
+
# Backend-only/custom expressions may have no generic compiler.
|
|
665
|
+
# Failing to prove equivalence must never remove an index.
|
|
666
|
+
continue
|
|
667
|
+
if not expressions:
|
|
668
|
+
continue
|
|
669
|
+
full_sig_map.setdefault((expressions, idx.unique, options), []).append(idx)
|
|
670
|
+
shape_map.setdefault((expressions, options), []).append(idx)
|
|
607
671
|
|
|
608
672
|
to_remove: set = set()
|
|
609
673
|
|
|
610
|
-
# Remove exact duplicates
|
|
674
|
+
# Remove exact duplicates (same expressions, unique flag, and options):
|
|
675
|
+
# prefer explicit names over generated names. Sort by name
|
|
676
|
+
# so the kept index is deterministic regardless of set iteration order.
|
|
611
677
|
for indexes in full_sig_map.values():
|
|
612
678
|
if len(indexes) > 1:
|
|
613
|
-
explicit =
|
|
614
|
-
|
|
679
|
+
explicit = sorted(
|
|
680
|
+
(
|
|
681
|
+
i
|
|
682
|
+
for i in indexes
|
|
683
|
+
if not (isinstance(i.name, conv) or (i.name and i.name.startswith(_TEMP_INDEX_PREFIX)))
|
|
684
|
+
),
|
|
685
|
+
key=lambda i: i.name or "",
|
|
686
|
+
)
|
|
687
|
+
keep = explicit[0] if explicit else sorted(indexes, key=lambda i: i.name or "")[0]
|
|
615
688
|
to_remove.update(i for i in indexes if i is not keep)
|
|
616
689
|
|
|
617
|
-
#
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
690
|
+
# A full-table unique index can replace a plain index only when its
|
|
691
|
+
# ordered expressions AND options match (including access method/INCLUDE).
|
|
692
|
+
# Partial indexes (unique or not) never participate: a partial unique index
|
|
693
|
+
# only covers rows matching its predicate and cannot replace a full index.
|
|
694
|
+
for indexes in shape_map.values():
|
|
695
|
+
if any(getattr(i, "unique", False) and not is_partial(i) for i in indexes):
|
|
696
|
+
to_remove.update(i for i in indexes if not getattr(i, "unique", False) and not is_partial(i))
|
|
621
697
|
|
|
622
698
|
for idx in to_remove:
|
|
623
699
|
table.indexes.discard(idx)
|
|
624
700
|
|
|
625
|
-
# Normalize
|
|
701
|
+
# Normalize generated names after dedup. Explicit names and SQLAlchemy
|
|
702
|
+
# naming conventions retain control over their own identifiers.
|
|
703
|
+
generated = []
|
|
704
|
+
explicit_names = {str(cst.name) for cst in table.constraints if cst.name is not None}
|
|
626
705
|
for idx in table.indexes:
|
|
627
706
|
if idx.name and idx.name.startswith(_TEMP_INDEX_PREFIX) and idx.columns:
|
|
628
|
-
|
|
707
|
+
name = f"idx_{table_name}_{'_'.join(col.name for col in idx.columns)}"
|
|
708
|
+
else:
|
|
709
|
+
if idx.name is not None:
|
|
710
|
+
explicit_names.add(str(idx.name))
|
|
711
|
+
continue
|
|
712
|
+
signature = repr((tuple(str(expr) for expr in idx.expressions), idx.unique, dialect_sig(idx)))
|
|
713
|
+
generated.append((idx, name, signature))
|
|
714
|
+
_assign_schema_names(generated, explicit_names)
|
|
629
715
|
|
|
630
716
|
@classmethod
|
|
631
717
|
def _post_process_table_constraints(mcs, table, table_name: str) -> None:
|
|
@@ -635,6 +721,8 @@ class ModelProcessor(type):
|
|
|
635
721
|
table: SQLAlchemy Table instance
|
|
636
722
|
table_name: Database table name
|
|
637
723
|
"""
|
|
724
|
+
generated = []
|
|
725
|
+
explicit_names = {str(item.name) for item in [*table.constraints, *table.indexes] if item.name is not None}
|
|
638
726
|
for cst in table.constraints:
|
|
639
727
|
if cst.name is None:
|
|
640
728
|
if isinstance(cst, CheckConstraint):
|
|
@@ -642,12 +730,14 @@ class ModelProcessor(type):
|
|
|
642
730
|
field_matches = _FIELD_NAME_PATTERN.findall(str(cst.sqltext))
|
|
643
731
|
if field_matches:
|
|
644
732
|
field_part = "_".join(field_matches[:2])
|
|
645
|
-
|
|
733
|
+
name = f"ck_{table_name}_{field_part}"
|
|
646
734
|
else:
|
|
647
|
-
|
|
735
|
+
name = f"ck_{table_name}_constraint"
|
|
736
|
+
signature = str(cst.sqltext.compile(compile_kwargs={"literal_binds": True}))
|
|
648
737
|
elif isinstance(cst, UniqueConstraint) and hasattr(cst, "columns"):
|
|
649
738
|
field_names = "_".join(col.name for col in cst.columns)
|
|
650
|
-
|
|
739
|
+
name = f"uq_{table_name}_{field_names}"
|
|
740
|
+
signature = field_names
|
|
651
741
|
elif isinstance(cst, ForeignKeyConstraint) and hasattr(cst, "columns"):
|
|
652
742
|
# Handle foreign key constraints
|
|
653
743
|
field_names = "_".join(col.name for col in cst.columns)
|
|
@@ -656,12 +746,17 @@ class ModelProcessor(type):
|
|
|
656
746
|
try:
|
|
657
747
|
ref_table = cst.elements[0].column.table.name
|
|
658
748
|
ref_columns = "_".join(elem.column.name for elem in cst.elements)
|
|
659
|
-
|
|
749
|
+
name = f"fk_{table_name}_{field_names}_{ref_table}_{ref_columns}"
|
|
660
750
|
except Exception:
|
|
661
751
|
# Fallback if reference cannot be resolved yet
|
|
662
|
-
|
|
752
|
+
name = f"fk_{table_name}_{field_names}"
|
|
663
753
|
else:
|
|
664
|
-
|
|
754
|
+
name = f"fk_{table_name}_{field_names}"
|
|
755
|
+
signature = repr(tuple(elem.target_fullname for elem in cst.elements))
|
|
756
|
+
else:
|
|
757
|
+
continue
|
|
758
|
+
generated.append((cst, name, signature))
|
|
759
|
+
_assign_schema_names(generated, explicit_names)
|
|
665
760
|
|
|
666
761
|
@classmethod
|
|
667
762
|
def _apply_dataclass_functionality(mcs, cls: Any) -> Any:
|