sqlobjects 2.0.1__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.
Files changed (42) hide show
  1. sqlobjects/__init__.py +1 -1
  2. sqlobjects/cascade.py +22 -31
  3. sqlobjects/contrib/asgi.py +5 -3
  4. sqlobjects/database/manager.py +44 -30
  5. sqlobjects/expressions/aggregate.py +144 -83
  6. sqlobjects/expressions/scalar.py +5 -9
  7. sqlobjects/fields/__init__.py +2 -0
  8. sqlobjects/fields/core.py +3 -2
  9. sqlobjects/fields/proxies.py +78 -30
  10. sqlobjects/fields/relations/descriptors.py +4 -2
  11. sqlobjects/fields/relations/prefetch.py +62 -63
  12. sqlobjects/fields/relations/utils.py +50 -11
  13. sqlobjects/internal/results.py +1 -1
  14. sqlobjects/metadata.py +106 -30
  15. sqlobjects/mixins.py +24 -5
  16. sqlobjects/model.py +45 -72
  17. sqlobjects/objects/bulk.py +159 -245
  18. sqlobjects/objects/core.py +62 -58
  19. sqlobjects/objects/upsert.py +85 -8
  20. sqlobjects/queries/builder.py +77 -145
  21. sqlobjects/queries/dialect.py +4 -4
  22. sqlobjects/queries/executor.py +105 -209
  23. sqlobjects/queryset.py +64 -45
  24. sqlobjects/session.py +342 -38
  25. sqlobjects/signals.py +60 -33
  26. sqlobjects/sql_logging.py +100 -11
  27. sqlobjects/utils/naming.py +17 -1
  28. {sqlobjects-2.0.1.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/01-database-session-guide.md +44 -5
  29. {sqlobjects-2.0.1.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/02-model-definition-guide.md +25 -0
  30. {sqlobjects-2.0.1.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/03-query-operations-guide.md +33 -4
  31. {sqlobjects-2.0.1.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/05-relationships-guide.md +37 -15
  32. {sqlobjects-2.0.1.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/06-validation-signals-guide.md +32 -0
  33. {sqlobjects-2.0.1.dist-info → sqlobjects-2.1.0.dist-info}/METADATA +9 -2
  34. sqlobjects-2.1.0.dist-info/RECORD +75 -0
  35. sqlobjects-2.0.1.dist-info/RECORD +0 -75
  36. {sqlobjects-2.0.1.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/04-crud-operations-guide.md +0 -0
  37. {sqlobjects-2.0.1.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/07-performance-guide.md +0 -0
  38. {sqlobjects-2.0.1.data → sqlobjects-2.1.0.data}/data/share/sqlobjects/rules/README.md +0 -0
  39. {sqlobjects-2.0.1.dist-info → sqlobjects-2.1.0.dist-info}/WHEEL +0 -0
  40. {sqlobjects-2.0.1.dist-info → sqlobjects-2.1.0.dist-info}/entry_points.txt +0 -0
  41. {sqlobjects-2.0.1.dist-info → sqlobjects-2.1.0.dist-info}/licenses/LICENSE +0 -0
  42. {sqlobjects-2.0.1.dist-info → sqlobjects-2.1.0.dist-info}/top_level.txt +0 -0
@@ -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
- """Dynamic query set proxy for lazy='dynamic' relationships."""
383
+ class RelatedQuerySet(BaseRelated[list[T]]):
384
+ """Build fresh, parent-scoped queries for lazy='dynamic' collections."""
384
385
 
385
- def __init__(self, instance: "ObjectModel", descriptor: "RelationshipDescriptor"):
386
- super().__init__(instance, descriptor)
387
- self.parent_instance = instance
388
- self.relationship_desc = descriptor
389
- self._queryset: Any = None
390
- self._initialized = False
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
- async def _load(self):
401
- """Load implementation - returns QuerySet."""
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
- def _set_empty_result(self):
406
- """Set empty QuerySet result."""
407
- self._cached_value = None
408
- self._loaded = True
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 all QuerySet methods."""
412
- qs = self._get_queryset()
413
- if qs is None:
414
- raise NotImplementedError("QuerySet not yet implemented")
415
- return getattr(qs, name)
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
- return RelatedQuerySet(instance, self)
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 or not prefetch_relationships:
12
+ async def handle_prefetch_relationships(self, instances, prefetch_relationships, configs=None):
13
+ if not instances:
14
14
  return instances
15
-
16
- for relationship_name in prefetch_relationships:
17
- relationship_info = RelationshipAnalyzer.analyze_relationship(instances[0].__class__, relationship_name)
18
- if relationship_info:
19
- await self._prefetch_single_relationship(instances, relationship_name, relationship_info)
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
- rel_type = relationship_info["type"]
25
-
26
- if rel_type == "reverse_fk":
27
- await self._prefetch_by_fields(
28
- instances,
29
- relationship_name,
30
- relationship_info["related_model"],
31
- relationship_info["ref_fields"],
32
- relationship_info["foreign_key_fields"],
33
- [],
34
- )
35
- elif rel_type == "one_to_one":
36
- info = relationship_info
37
- await self._prefetch_by_fields(
38
- instances,
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
- cols = [getattr(related_model, f) for f in group_fields]
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 = related_model.objects.using(self.session).filter(
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
- through_model = self._find_through_model(through_table)
115
- if not through_model:
116
- return
113
+ from sqlalchemy import select
117
114
 
118
- through_objects = await (
119
- through_model.objects.using(self.session)
120
- .filter(getattr(through_model, left_field).in_(instance_values))
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
- related_objects = await (
131
- related_model.objects.using(self.session)
132
- .filter(getattr(related_model, right_ref_field).in_(related_values))
133
- .all()
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
- try:
176
- if hasattr(model_class, relationship_name):
177
- field_attr = getattr(model_class, relationship_name)
178
- if hasattr(field_attr, "property"):
179
- return RelationshipAnalyzer._extract_relationship_info(model_class, field_attr.property)
180
- return RelationshipAnalyzer._infer_reverse_relationship(model_class, relationship_name)
181
- except Exception: # noqa
182
- return None
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 = RelationshipAnalyzer._resolve_model_class(prop.argument)
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
- return None
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
- return subclass.__registry__.get_model(argument)
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
 
@@ -51,7 +51,7 @@ class ResultProcessor:
51
51
  objects.append(filtered_dict)
52
52
  else:
53
53
  # Return full model instance
54
- obj = model_class.from_dict(row_dict, validate=False)
54
+ obj = model_class._from_database(row_dict)
55
55
  objects.append(obj)
56
56
 
57
57
  return objects
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 direct_objectmodel_bases:
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
- index_name = f"idx_{table_name}_{name}"
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
- index_name = f"idx_{table_name}_{name}"
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,59 +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 col_sig(idx):
596
- return tuple(sorted(col.name for col in idx.columns)) if idx.columns else None
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
+ )
597
628
 
598
629
  def dialect_sig(idx):
599
630
  # Dialect kwargs (postgresql_where / postgresql_using / mysql_using, etc.)
600
631
  # are part of an index's identity: same columns with different partial
601
632
  # predicates or access methods are distinct indexes, not duplicates.
602
- # Values may be str or SQLAlchemy expressions, so compare via str().
633
+ # Include literal values when comparing SQLAlchemy predicates.
603
634
  # dialect_kwargs only holds explicitly passed args, not dialect defaults.
604
635
  kwargs = getattr(idx, "dialect_kwargs", None) or {}
605
- return tuple(sorted((k, str(v)) for k, v in kwargs.items() if v is not None))
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))
606
649
 
607
650
  def is_partial(idx):
608
- return any(k.endswith("_where") for k, _ in dialect_sig(idx))
651
+ return any(k.endswith("_where") and value is not None for k, value in idx.dialect_kwargs.items())
609
652
 
610
- full_sig_map: dict[tuple, list] = {} # (cols, unique, dialect_sig) -> indexes
611
- col_map: dict[tuple, list] = {} # cols -> indexes
653
+ full_sig_map: dict[tuple, list] = {} # (expressions, unique, options) -> indexes
654
+ shape_map: dict[tuple, list] = {} # (expressions, options) -> indexes
612
655
 
613
656
  for idx in list(table.indexes):
614
- cs = col_sig(idx)
615
- if cs is None:
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:
616
660
  continue
617
- full_sig_map.setdefault((cs, getattr(idx, "unique", False), dialect_sig(idx)), []).append(idx)
618
- col_map.setdefault(cs, []).append(idx)
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)
619
671
 
620
672
  to_remove: set = set()
621
673
 
622
- # Remove exact duplicates (same columns, unique flag, and dialect kwargs):
623
- # prefer explicit names over SQLAlchemy auto-generated "ix_*". Sort by name
674
+ # Remove exact duplicates (same expressions, unique flag, and options):
675
+ # prefer explicit names over generated names. Sort by name
624
676
  # so the kept index is deterministic regardless of set iteration order.
625
677
  for indexes in full_sig_map.values():
626
678
  if len(indexes) > 1:
627
679
  explicit = sorted(
628
- (i for i in indexes if not (i.name and i.name.startswith("ix_"))),
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
+ ),
629
685
  key=lambda i: i.name or "",
630
686
  )
631
687
  keep = explicit[0] if explicit else sorted(indexes, key=lambda i: i.name or "")[0]
632
688
  to_remove.update(i for i in indexes if i is not keep)
633
689
 
634
- # Remove full-table non-unique when a full-table unique exists on same columns.
690
+ # A full-table unique index can replace a plain index only when its
691
+ # ordered expressions AND options match (including access method/INCLUDE).
635
692
  # Partial indexes (unique or not) never participate: a partial unique index
636
693
  # only covers rows matching its predicate and cannot replace a full index.
637
- for indexes in col_map.values():
694
+ for indexes in shape_map.values():
638
695
  if any(getattr(i, "unique", False) and not is_partial(i) for i in indexes):
639
696
  to_remove.update(i for i in indexes if not getattr(i, "unique", False) and not is_partial(i))
640
697
 
641
698
  for idx in to_remove:
642
699
  table.indexes.discard(idx)
643
700
 
644
- # Normalize temp names after dedup
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}
645
705
  for idx in table.indexes:
646
706
  if idx.name and idx.name.startswith(_TEMP_INDEX_PREFIX) and idx.columns:
647
- idx.name = f"idx_{table_name}_{'_'.join(col.name for col in idx.columns)}"
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)
648
715
 
649
716
  @classmethod
650
717
  def _post_process_table_constraints(mcs, table, table_name: str) -> None:
@@ -654,6 +721,8 @@ class ModelProcessor(type):
654
721
  table: SQLAlchemy Table instance
655
722
  table_name: Database table name
656
723
  """
724
+ generated = []
725
+ explicit_names = {str(item.name) for item in [*table.constraints, *table.indexes] if item.name is not None}
657
726
  for cst in table.constraints:
658
727
  if cst.name is None:
659
728
  if isinstance(cst, CheckConstraint):
@@ -661,12 +730,14 @@ class ModelProcessor(type):
661
730
  field_matches = _FIELD_NAME_PATTERN.findall(str(cst.sqltext))
662
731
  if field_matches:
663
732
  field_part = "_".join(field_matches[:2])
664
- cst.name = f"ck_{table_name}_{field_part}"
733
+ name = f"ck_{table_name}_{field_part}"
665
734
  else:
666
- cst.name = f"ck_{table_name}_constraint"
735
+ name = f"ck_{table_name}_constraint"
736
+ signature = str(cst.sqltext.compile(compile_kwargs={"literal_binds": True}))
667
737
  elif isinstance(cst, UniqueConstraint) and hasattr(cst, "columns"):
668
738
  field_names = "_".join(col.name for col in cst.columns)
669
- cst.name = f"uq_{table_name}_{field_names}"
739
+ name = f"uq_{table_name}_{field_names}"
740
+ signature = field_names
670
741
  elif isinstance(cst, ForeignKeyConstraint) and hasattr(cst, "columns"):
671
742
  # Handle foreign key constraints
672
743
  field_names = "_".join(col.name for col in cst.columns)
@@ -675,12 +746,17 @@ class ModelProcessor(type):
675
746
  try:
676
747
  ref_table = cst.elements[0].column.table.name
677
748
  ref_columns = "_".join(elem.column.name for elem in cst.elements)
678
- cst.name = f"fk_{table_name}_{field_names}_{ref_table}_{ref_columns}"
749
+ name = f"fk_{table_name}_{field_names}_{ref_table}_{ref_columns}"
679
750
  except Exception:
680
751
  # Fallback if reference cannot be resolved yet
681
- cst.name = f"fk_{table_name}_{field_names}"
752
+ name = f"fk_{table_name}_{field_names}"
682
753
  else:
683
- cst.name = f"fk_{table_name}_{field_names}"
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)
684
760
 
685
761
  @classmethod
686
762
  def _apply_dataclass_functionality(mcs, cls: Any) -> Any: