erdify 1.4.3__tar.gz → 1.4.4__tar.gz

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 (30) hide show
  1. {erdify-1.4.3 → erdify-1.4.4}/PKG-INFO +1 -1
  2. {erdify-1.4.3 → erdify-1.4.4}/pyproject.toml +1 -1
  3. {erdify-1.4.3 → erdify-1.4.4}/pyproject.toml.orig +1 -1
  4. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/core.py +1 -2
  5. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/dialects/base.py +52 -32
  6. erdify-1.4.4/src/erdify/parser/dialects/sqlalchemy.py +764 -0
  7. erdify-1.4.3/src/erdify/parser/dialects/sqlalchemy.py +0 -280
  8. {erdify-1.4.3 → erdify-1.4.4}/LICENSE +0 -0
  9. {erdify-1.4.3 → erdify-1.4.4}/README.md +0 -0
  10. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/__init__.py +0 -0
  11. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/__main__.py +0 -0
  12. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/cli.py +0 -0
  13. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/config.py +0 -0
  14. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/generator.py +0 -0
  15. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/inject.py +0 -0
  16. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/__init__.py +0 -0
  17. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/aliases.py +0 -0
  18. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/annotations.py +0 -0
  19. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/common.py +0 -0
  20. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/dialects/__init__.py +0 -0
  21. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/dialects/annotated.py +0 -0
  22. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/dialects/django.py +0 -0
  23. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/dialects/django_apps.py +0 -0
  24. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/dialects/django_contrib.py +0 -0
  25. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/dialects/keyless.py +0 -0
  26. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/discovery.py +0 -0
  27. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/parser/inheritance.py +0 -0
  28. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/py.typed +0 -0
  29. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/pyproject.py +0 -0
  30. {erdify-1.4.3 → erdify-1.4.4}/src/erdify/sql_parser.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: erdify
3
- Version: 1.4.3
3
+ Version: 1.4.4
4
4
  Summary: A zero-dependency ERD generator for your Python models — pure AST, no database required.
5
5
  Keywords: sqlmodel,sqlalchemy,django,pydantic,dataclass,erd,plantuml,diagram,database,erdify,sql,ddl
6
6
  Author: devsuit GmbH, Fabian Clemenz
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "erdify"
3
- version = "1.4.3"
3
+ version = "1.4.4"
4
4
  description = "A zero-dependency ERD generator for your Python models — pure AST, no database required."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "erdify"
3
- version = "1.4.3"
3
+ version = "1.4.4"
4
4
  description = "A zero-dependency ERD generator for your Python models — pure AST, no database required."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -172,8 +172,7 @@ class ASTDatabaseParser:
172
172
  continue
173
173
  self._parse_table_class(class_node, source)
174
174
 
175
- # Entities that are not classes, e.g. SQLAlchemy Core association
176
- # tables referenced by relationship(secondary=...) (#34).
175
+ # Entities that are not classes, e.g. SQLAlchemy Core tables (#34, #305).
177
176
  for dialect in self._ctx.all_dialects():
178
177
  if self.sources is not None and dialect.kind not in self.sources:
179
178
  continue
@@ -371,37 +371,57 @@ class Dialect:
371
371
  and node.target.id == "__table_args__"
372
372
  ):
373
373
  value = node.value
374
- columns: set[str] = set()
375
- refs: set[str] = set()
376
374
  if not isinstance(value, (ast.Tuple, ast.List)):
377
- return columns, refs
378
-
379
- for item in value.elts:
380
- if is_call_to(item, "UniqueConstraint"):
381
- cols = item.args
382
- elif is_call_to(item, "Index"):
383
- if not any(
384
- kw.arg == "unique" and isinstance(kw.value, ast.Constant) and kw.value.value
385
- for kw in item.keywords
386
- ):
387
- continue
388
- # A partial index (postgresql_where=, sqlite_where=, ...) is only
389
- # unique over a subset of rows, so it does not imply a 1:1. A
390
- # literal `*_where=None` is "no predicate", i.e. not partial.
391
- if any(
392
- kw.arg
393
- and kw.arg.endswith("_where")
394
- and not (isinstance(kw.value, ast.Constant) and kw.value.value is None)
395
- for kw in item.keywords
396
- ):
397
- continue
398
- cols = item.args[1:] # first positional arg is the index name
399
- else:
375
+ return set(), set()
376
+ return single_column_uniques(value.elts)
377
+
378
+
379
+ def explicit_column_name(value: ast.expr | None, use_key: bool = True) -> str | None:
380
+ """The explicit name (or, with ``use_key``, key) of a column declaration, if any.
381
+
382
+ See :meth:`Dialect._explicit_column_name`; also reads a Core ``Column(...)``.
383
+ """
384
+ return Dialect._explicit_column_name(value, use_key)
385
+
386
+
387
+ def single_column_uniques(items: Iterable[ast.expr]) -> Tuple[Set[str], Set[str]]:
388
+ """Columns made unique on their own by ``UniqueConstraint`` / unique ``Index`` items.
389
+
390
+ The items of a ``__table_args__`` tuple or the arguments of a Core
391
+ ``Table(...)``. Only constraints and indexes over exactly one column
392
+ count; a partial index (``*_where=``) does not make a column unique.
393
+
394
+ Returns ``(db_columns, attribute_refs)``: a string names the database
395
+ column, a bare name an attribute declared earlier in the class body.
396
+ """
397
+ columns: Set[str] = set()
398
+ refs: Set[str] = set()
399
+ for item in items:
400
+ if is_call_to(item, "UniqueConstraint"):
401
+ cols = item.args
402
+ elif is_call_to(item, "Index"):
403
+ if not any(
404
+ kw.arg == "unique" and isinstance(kw.value, ast.Constant) and kw.value.value
405
+ for kw in item.keywords
406
+ ):
400
407
  continue
401
- if len(cols) == 1:
402
- col = cols[0]
403
- if isinstance(col, ast.Constant) and isinstance(col.value, str):
404
- columns.add(col.value)
405
- elif isinstance(col, ast.Name):
406
- refs.add(col.id)
407
- return columns, refs
408
+ # A partial index (postgresql_where=, sqlite_where=, ...) is only
409
+ # unique over a subset of rows, so it does not imply a 1:1. A
410
+ # literal `*_where=None` is "no predicate", i.e. not partial.
411
+ if any(
412
+ kw.arg
413
+ and kw.arg.endswith("_where")
414
+ and not (isinstance(kw.value, ast.Constant) and kw.value.value is None)
415
+ for kw in item.keywords
416
+ ):
417
+ continue
418
+ cols = item.args[1:] # first positional arg is the index name
419
+ else:
420
+ continue
421
+ if len(cols) == 1:
422
+ col = cols[0]
423
+ if isinstance(col, ast.Constant) and isinstance(col.value, str):
424
+ columns.add(col.value)
425
+ elif isinstance(col, ast.Name):
426
+ refs.add(col.id)
427
+ return columns, refs
@@ -0,0 +1,764 @@
1
+ """SQLModel and SQLAlchemy 2.0 (ORM classes, classes mapped to Core tables, Core tables)."""
2
+
3
+ import ast
4
+ from typing import Callable, Dict, Iterable, List, Mapping, NamedTuple, Tuple
5
+
6
+ from ...config import EntityInfo, FieldInfo, is_structural_link_table
7
+ from ..annotations import expr_name, is_call_to, is_mapped_annotation
8
+ from .annotated import AnnotatedDialect, extract_foreign_key_arg
9
+ from .base import ParseContext, explicit_column_name, single_column_uniques
10
+
11
+
12
+ class TableDialect(AnnotatedDialect):
13
+ """Shared by SQLModel and SQLAlchemy: classes mapped to tables.
14
+
15
+ An explicit column name (``mapped_column("name")``, ``name=``,
16
+ ``Field(sa_column=Column("name"))``, ``sa_column_kwargs={"name": ...}``)
17
+ names the database column, so the column is drawn under it (#292).
18
+ """
19
+
20
+ def __init__(self, ctx: ParseContext):
21
+ super().__init__(ctx)
22
+ #: Table name -> {column key: column name} where an explicit ``key=``
23
+ #: differs from the name (``mapped_column("account_id", key="id")``).
24
+ self._renamed_keys: Dict[str, Dict[str, str]] = {}
25
+ #: id() of foreign-key fields declared with ``link_to_name=True``: their
26
+ #: target already names the column, not its key.
27
+ # Keyed by id() but holding the object, so a freed object's address is
28
+ # never matched by a new one.
29
+ self._name_targets: Dict[int, FieldInfo] = {}
30
+ #: id() of Core tables declared on ``SQLModel.metadata``: they belong
31
+ #: to SQLModel's tables, although they are read as SQLAlchemy.
32
+ self._sqlmodel_core: Dict[int, EntityInfo] = {}
33
+
34
+ def _parse_field(self, node: ast.AnnAssign) -> FieldInfo | None:
35
+ """Parse a column, remembering a ``ForeignKey(..., link_to_name=True)``."""
36
+ field = super()._parse_field(node)
37
+ if field is not None and field.is_foreign_key and _links_to_name(node):
38
+ self._name_targets[id(field)] = field
39
+ return field
40
+
41
+ def finish(self, entity: EntityInfo, class_node: ast.ClassDef) -> None:
42
+ """Mark ``__table_args__`` uniques, then rename columns to their explicit names."""
43
+ # The unique constraints are matched by attribute name and key first.
44
+ super().finish(entity, class_node)
45
+ keys = self._db_column_names(class_node)
46
+ names = self._db_column_names(class_node, use_key=False)
47
+ renamed = {
48
+ keys[attr]: name for attr, name in names.items() if attr in keys and keys[attr] != name
49
+ }
50
+ if renamed:
51
+ self._renamed_keys[entity.table_name] = renamed
52
+ for f in entity.fields:
53
+ f.name = names.get(f.name, f.name)
54
+
55
+ def finalize(self, entities: Mapping[str, EntityInfo]) -> None:
56
+ """Point foreign keys written against a column key at the column's name.
57
+
58
+ SQLAlchemy resolves ``ForeignKey("account.id")`` through the column
59
+ key, so with ``mapped_column("account_id", key="id")`` it references
60
+ ``account_id``.
61
+ """
62
+ # Only this dialect's tables: SQLModel and SQLAlchemy keep separate
63
+ # metadata, so a table name may exist in both. A Core table belongs to
64
+ # the metadata it is declared on.
65
+ renamed = self._renamed_keys
66
+ if not renamed:
67
+ return
68
+ sqlmodel_core: Dict[int, EntityInfo] = {}
69
+ name_targets: Dict[int, FieldInfo] = {}
70
+ for dialect in self.ctx.all_dialects():
71
+ if isinstance(dialect, TableDialect):
72
+ sqlmodel_core.update(dialect._sqlmodel_core)
73
+ name_targets.update(dialect._name_targets)
74
+ for entity in entities.values():
75
+ kind = "sqlmodel" if sqlmodel_core.get(id(entity)) is entity else entity.source
76
+ if kind != self.kind:
77
+ continue
78
+ for f in entity.fields:
79
+ if f.foreign_table is None or name_targets.get(id(f)) is f:
80
+ continue
81
+ table, _, column = f.foreign_table.rpartition(".")
82
+ name = renamed.get(table, {}).get(column)
83
+ if name is not None:
84
+ f.foreign_table = f"{table}.{name}"
85
+
86
+
87
+ class SQLModelDialect(TableDialect):
88
+ """SQLModel: a class declared with ``table=True``."""
89
+
90
+ kind = "sqlmodel"
91
+
92
+ def detect(self, class_node: ast.ClassDef) -> bool:
93
+ for keyword in class_node.keywords:
94
+ if keyword.arg == "table" and isinstance(keyword.value, ast.Constant):
95
+ if keyword.value.value:
96
+ return True
97
+ return False
98
+
99
+ def default_table_name(self, class_node: ast.ClassDef) -> str:
100
+ """SQLModel's default ``__tablename__``: the lower-cased class name (#291).
101
+
102
+ ``HeroTeam`` -> ``heroteam``, not snake_case; foreign keys written as
103
+ ``foreign_key="heroteam.id"`` must find it.
104
+ """
105
+ return class_node.name.lower()
106
+
107
+
108
+ class SQLAlchemyDialect(TableDialect):
109
+ """SQLAlchemy 2.0: a concrete mapped class has ``__tablename__`` and ``Mapped[...]``.
110
+
111
+ Mixins / abstract bases (``Mapped`` fields but no ``__tablename__``) are not
112
+ detected, so they are not emitted as entities, but their fields are still
113
+ inherited. A class mapped to a Core table, through ``__table__ = Table(...)``
114
+ (#297) or ``registry.map_imperatively(Class, table)`` (#306), is drawn with
115
+ that table's name and columns. Every other module-level Core ``Table(...)``
116
+ is drawn as a table of its own (#34, #305).
117
+ """
118
+
119
+ kind = "sqlalchemy"
120
+ reads_annotated_columns = True
121
+ plain_value_is_column = False
122
+
123
+ def __init__(self, ctx: ParseContext):
124
+ super().__init__(ctx)
125
+ self._mapped: Dict[str, _MappedTable] | None = None
126
+ #: Entity name -> the ``properties=`` of its ``map_imperatively()`` call.
127
+ self._properties: Dict[str, ast.Dict] = {}
128
+ #: Core columns declared with only a ``ForeignKey``: SQLAlchemy gives
129
+ #: them the referenced column's type.
130
+ self._untyped: List[Tuple[FieldInfo, str]] = []
131
+
132
+ @property
133
+ def _mapped_tables(self) -> Dict[str, "_MappedTable"]:
134
+ """Class name -> the Core table it is mapped to (scanned once per run)."""
135
+ if self._mapped is None:
136
+ self._mapped = mapped_tables(self.ctx.trees, self.classes.all, self.ctx.tree_of)
137
+ return self._mapped
138
+
139
+ def detect(self, class_node: ast.ClassDef) -> bool:
140
+ if class_node.name in self._mapped_tables:
141
+ return True
142
+ return has_tablename(class_node) and has_mapped_field(class_node)
143
+
144
+ def table_name(self, class_node: ast.ClassDef) -> str | None:
145
+ """The mapped Core table's name, else ``__tablename__``."""
146
+ mapped = self._mapped_tables.get(class_node.name)
147
+ if mapped is not None:
148
+ name = core_table_name(mapped.table)
149
+ if name is not None:
150
+ return name
151
+ return super().table_name(class_node)
152
+
153
+ def finish(self, entity: EntityInfo, class_node: ast.ClassDef) -> None:
154
+ """A class mapped to a Core table has exactly that table's columns."""
155
+ mapped = self._mapped_tables.get(class_node.name)
156
+ if mapped is None:
157
+ super().finish(entity, class_node)
158
+ return
159
+ entity.fields = core_table_fields(mapped.table)
160
+ self._note_core_table(entity, mapped.table)
161
+ if mapped.properties is not None:
162
+ self._properties[entity.name] = mapped.properties
163
+
164
+ def extra_entities(self, trees: Iterable[ast.Module]) -> List[EntityInfo]:
165
+ """Module-level Core tables no class is mapped to (#34, #305)."""
166
+ mapped = {id(m.table) for m in self._mapped_tables.values()}
167
+ entities: List[EntityInfo] = []
168
+ for tree in trees:
169
+ for var_name, call in module_tables(tree):
170
+ if id(call) in mapped:
171
+ continue
172
+ entity = build_core_table_entity(var_name, call)
173
+ if entity is not None:
174
+ self._note_core_table(entity, call)
175
+ entities.append(entity)
176
+ return entities
177
+
178
+ def finalize(self, entities: Mapping[str, EntityInfo]) -> None:
179
+ """Rename keys, type ``ForeignKey``-only Core columns, add imperative relationships."""
180
+ super().finalize(entities)
181
+ # SQLModel and SQLAlchemy keep separate metadata: a foreign key only
182
+ # references a table of its own kind (a Core table on SQLModel.metadata
183
+ # is SQLModel's).
184
+ # Several MetaData collections of one kind may define the same table:
185
+ # such a reference is ambiguous (None) and keeps no type.
186
+ columns: Dict[str, Dict[str, FieldInfo | None]] = {}
187
+ for entity in entities.values():
188
+ kind = self._metadata_kind(entity)
189
+ for f in entity.fields:
190
+ ref = f"{entity.table_name}.{f.name}"
191
+ known = columns.setdefault(kind, {})
192
+ known[ref] = None if ref in known else f
193
+ # A chain of ForeignKey-only columns takes the type from its end.
194
+ changed = True
195
+ while changed:
196
+ changed = False
197
+ for f, kind in self._untyped:
198
+ target = columns.get(kind, {}).get(f.foreign_table or "")
199
+ if not f.type_str and target is not None and target.type_str:
200
+ f.type_str = target.type_str
201
+ changed = True
202
+ for name, properties in self._properties.items():
203
+ mapped_entity = entities.get(name)
204
+ if mapped_entity is not None:
205
+ self._add_properties(mapped_entity, properties, entities)
206
+
207
+ def _metadata_kind(self, entity: EntityInfo) -> str:
208
+ """The metadata an entity's table is on: its source, or SQLModel's for a Core table."""
209
+ return "sqlmodel" if self._sqlmodel_core.get(id(entity)) is entity else entity.source
210
+
211
+ def _add_properties(
212
+ self, entity: EntityInfo, properties: ast.Dict, entities: Mapping[str, EntityInfo]
213
+ ) -> None:
214
+ """The ``relationship()`` entries of ``map_imperatively(properties={...})``.
215
+
216
+ There is no annotation to read, so the cardinality is inferred the way
217
+ SQLAlchemy does: a foreign key from this table to the target's makes a
218
+ many-to-one, otherwise it is one-to-many (``uselist=False``: one-to-one).
219
+ """
220
+ for key, value in zip(properties.keys, properties.values):
221
+ if not (isinstance(key, ast.Constant) and isinstance(key.value, str)):
222
+ continue
223
+ if not is_call_to(value, "relationship") or not isinstance(value, ast.Call):
224
+ continue
225
+ # An option set to None or an empty collection is SQLAlchemy's default.
226
+ options = {kw.arg: kw.value for kw in value.keywords if kw.arg and not _unset(kw.value)}
227
+ # A secondary= many-to-many is drawn through its association table.
228
+ if "secondary" in options:
229
+ continue
230
+ target = _relationship_target(value)
231
+ if target is None:
232
+ continue
233
+ uselist = options.get("uselist")
234
+ target_entity = entities.get(target)
235
+ if target == entity.name:
236
+ # Self-referential: one-to-many unless remote_side= names the
237
+ # referenced side; naming the foreign-key columns keeps it one-to-many.
238
+ own = _references(entity, entity.table_name)
239
+ remote = options.get("remote_side")
240
+ names = _column_names(remote) if remote is not None else set()
241
+ on_foreign_key = bool(names) and names <= {f.name for f in own}
242
+ references: List[FieldInfo] = (
243
+ own if remote is not None and not on_foreign_key else []
244
+ )
245
+ elif target_entity is not None:
246
+ references = _references(entity, target_entity.table_name)
247
+ else:
248
+ references = []
249
+ explicit = uselist.value if isinstance(uselist, ast.Constant) else None
250
+ if explicit is True: # uselist=True: a collection whatever the direction
251
+ kind, nullable = "many", False
252
+ elif references:
253
+ kind, nullable = "one", any(f.is_nullable for f in references)
254
+ elif explicit is False:
255
+ kind, nullable = "one", True
256
+ else:
257
+ kind, nullable = "many", False
258
+ entity.relationships.append((target, kind, key.value))
259
+ if nullable:
260
+ entity.nullable_relationships.append(key.value)
261
+ inverse = self._back_populates(value)
262
+ if inverse is not None:
263
+ entity.inverse_relationships[key.value] = inverse
264
+
265
+ def _is_sqlmodel_metadata(self, metadata: ast.expr) -> bool:
266
+ """Whether ``metadata`` is ``SQLModel.metadata``, also through a scanned subclass."""
267
+ if not (isinstance(metadata, ast.Attribute) and metadata.attr == "metadata"):
268
+ return False
269
+ owner = expr_name(metadata.value)
270
+ if owner == "SQLModel":
271
+ return True
272
+ node = self.classes.all.get(owner) if owner is not None else None
273
+ return node is not None and any(
274
+ expr_name(base) == "SQLModel"
275
+ for name in self.classes.dfs_order(node, set())
276
+ for base in self.classes.all[name].bases
277
+ )
278
+
279
+ def _note_core_table(self, entity: EntityInfo, call: ast.Call) -> None:
280
+ """Record what :meth:`finalize` needs: the metadata, keys, ``link_to_name``, types."""
281
+ owner: TableDialect = self
282
+ if len(call.args) > 1 and self._is_sqlmodel_metadata(call.args[1]):
283
+ self._sqlmodel_core[id(entity)] = entity
284
+ owner = next(d for d in self.ctx.all_dialects() if isinstance(d, SQLModelDialect))
285
+ columns = {f.name: f for f in entity.fields}
286
+ renamed: Dict[str, str] = {}
287
+ for arg in call.args:
288
+ # ForeignKeyConstraint(["col"], ["t.name"], link_to_name=True): the
289
+ # local column is named through its key, the target by its name.
290
+ if is_call_to(arg, "ForeignKeyConstraint") and isinstance(arg, ast.Call):
291
+ local = _strings_of(arg.args[0]) if arg.args else []
292
+ field = columns.get(renamed_back(call, local[0])) if len(local) == 1 else None
293
+ if field is not None and any(
294
+ kw.arg == "link_to_name"
295
+ and isinstance(kw.value, ast.Constant)
296
+ and kw.value.value is True
297
+ for kw in arg.keywords
298
+ ):
299
+ self._name_targets[id(field)] = field
300
+ continue
301
+ if not is_call_to(arg, "Column") or not isinstance(arg, ast.Call):
302
+ continue
303
+ name = self._explicit_column_name(arg, use_key=False)
304
+ key = self._explicit_column_name(arg)
305
+ field = columns.get(name or "")
306
+ if field is None:
307
+ continue
308
+ if key is not None and key != name:
309
+ renamed[key] = field.name
310
+ if _links_to_name(arg):
311
+ self._name_targets[id(field)] = field
312
+ if field.is_foreign_key and not field.type_str:
313
+ self._untyped.append((field, self._metadata_kind(entity)))
314
+ if renamed:
315
+ owner._renamed_keys.setdefault(entity.table_name, {}).update(renamed)
316
+
317
+
318
+ def renamed_back(call: ast.Call, key: str) -> str:
319
+ """The name of the column of a Core ``Table(...)`` whose key is ``key``."""
320
+ for arg in call.args:
321
+ if is_call_to(arg, "Column") and isinstance(arg, ast.Call):
322
+ if explicit_column_name(arg) == key:
323
+ return explicit_column_name(arg, use_key=False) or key
324
+ return key
325
+
326
+
327
+ class _MappedTable(NamedTuple):
328
+ """The Core table a class is mapped to."""
329
+
330
+ table: ast.Call
331
+ #: ``map_imperatively(..., properties={...})``, if given as a dict literal.
332
+ properties: ast.Dict | None = None
333
+
334
+
335
+ def module_tables(tree: ast.Module) -> List[Tuple[str, ast.Call]]:
336
+ """``(variable, call)`` for each module-level ``name = Table(...)``, in source order.
337
+
338
+ An annotated assignment (``users: Table = Table(...)``) counts too.
339
+ """
340
+ found: List[Tuple[str, ast.Call]] = []
341
+ for node in tree.body:
342
+ if isinstance(node, ast.Assign) and len(node.targets) == 1:
343
+ target, value = node.targets[0], node.value
344
+ elif isinstance(node, ast.AnnAssign) and node.value is not None:
345
+ target, value = node.target, node.value
346
+ else:
347
+ continue
348
+ if (
349
+ isinstance(target, ast.Name)
350
+ and isinstance(value, ast.Call)
351
+ and is_call_to(value, "Table")
352
+ and _has_metadata(value)
353
+ ):
354
+ found.append((target.id, value))
355
+ return found
356
+
357
+
358
+ def _has_metadata(call: ast.Call) -> bool:
359
+ """Whether a ``Table(...)`` call passes a metadata: SQLAlchemy requires one.
360
+
361
+ Tells a Core table from another library's ``Table`` (``rich.table.Table("T",
362
+ Column("Age"))``), whose second argument is no metadata.
363
+ """
364
+ if any(kw.arg == "metadata" for kw in call.keywords):
365
+ return True
366
+ return len(call.args) > 1 and not (
367
+ isinstance(call.args[1], ast.Constant) or is_call_to(call.args[1], "Column")
368
+ )
369
+
370
+
371
+ def mapped_tables(
372
+ trees: Iterable[ast.Module],
373
+ classes: Mapping[str, ast.ClassDef],
374
+ tree_of: Callable[[ast.ClassDef], ast.Module | None],
375
+ ) -> Dict[str, _MappedTable]:
376
+ """Classes mapped to a Core table, by class name.
377
+
378
+ ``class X(Base): __table__ = Table(...)`` (or a module-level table
379
+ variable, #297), and ``registry.map_imperatively(X, table, properties=...)``
380
+ anywhere in a scanned file, e.g. inside a ``start_mappers()`` function (#306).
381
+ A table variable is looked up in the same file first, then in the one
382
+ scanned file that defines it (not when several do).
383
+ """
384
+ trees = list(trees)
385
+ per_tree = {id(tree): dict(module_tables(tree)) for tree in trees}
386
+ # A variable defined in several files is ambiguous elsewhere: not resolved.
387
+ everywhere: Dict[str, ast.Call | None] = {}
388
+ for tree in trees:
389
+ for name, call in per_tree[id(tree)].items():
390
+ everywhere[name] = None if name in everywhere else call
391
+
392
+ def resolve(value: ast.expr | None, tree: ast.Module | None) -> ast.Call | None:
393
+ if isinstance(value, ast.Call) and is_call_to(value, "Table"):
394
+ return value
395
+ if isinstance(value, ast.Name):
396
+ local = per_tree.get(id(tree), {}) if tree is not None else {}
397
+ return local.get(value.id) or everywhere.get(value.id)
398
+ if isinstance(value, ast.Attribute): # tables.batches
399
+ return everywhere.get(value.attr)
400
+ return None
401
+
402
+ def class_name(value: ast.expr | None) -> str | None:
403
+ """The mapped class, also qualified (``model.Batch`` from a domain module)."""
404
+ name = expr_name(value) if isinstance(value, (ast.Name, ast.Attribute)) else None
405
+ name = name.rpartition(".")[2] if name is not None else None
406
+ return name if name in classes else None
407
+
408
+ found: Dict[str, _MappedTable] = {}
409
+ for name, class_node in classes.items():
410
+ for node in class_node.body:
411
+ # __table__ = Table(...), also annotated (__table__: Table = ...).
412
+ targets = (
413
+ node.targets
414
+ if isinstance(node, ast.Assign)
415
+ else [node.target]
416
+ if isinstance(node, ast.AnnAssign)
417
+ else []
418
+ )
419
+ if any(isinstance(t, ast.Name) and t.id == "__table__" for t in targets):
420
+ assert isinstance(node, (ast.Assign, ast.AnnAssign))
421
+ table = resolve(node.value, tree_of(class_node))
422
+ if table is not None:
423
+ found[name] = _MappedTable(table)
424
+ for tree in trees:
425
+ for mapping in ast.walk(tree):
426
+ if not (
427
+ isinstance(mapping, ast.Call)
428
+ and isinstance(mapping.func, ast.Attribute)
429
+ and mapping.func.attr == "map_imperatively"
430
+ ):
431
+ continue
432
+ options = {kw.arg: kw.value for kw in mapping.keywords if kw.arg}
433
+ args = list(mapping.args)
434
+ mapped = class_name(args[0] if args else options.get("class_"))
435
+ if mapped is None:
436
+ continue
437
+ table = resolve(args[1] if len(args) > 1 else options.get("local_table"), tree)
438
+ if table is None:
439
+ continue # e.g. single-table inheritance: no table of its own
440
+ properties = options.get("properties")
441
+ found[mapped] = _MappedTable(
442
+ table, properties if isinstance(properties, ast.Dict) else None
443
+ )
444
+ return found
445
+
446
+
447
+ def _column_names(value: ast.expr) -> set[str]:
448
+ """Column names a ``remote_side=`` names: ``[t.c.id]``, ``"Node.id"``, ``[id]``."""
449
+ if isinstance(value, ast.Lambda):
450
+ value = value.body
451
+ items = value.elts if isinstance(value, (ast.List, ast.Tuple, ast.Set)) else [value]
452
+ names: set[str] = set()
453
+ for item in items:
454
+ if isinstance(item, ast.Constant) and isinstance(item.value, str):
455
+ names.add(item.value.rpartition(".")[2])
456
+ elif isinstance(item, ast.Attribute):
457
+ names.add(item.attr)
458
+ elif isinstance(item, ast.Name):
459
+ names.add(item.id)
460
+ return names
461
+
462
+
463
+ def _unset(value: ast.expr) -> bool:
464
+ """``None`` or an empty list / tuple: an option left at its default."""
465
+ if isinstance(value, ast.Constant):
466
+ return value.value is None
467
+ return isinstance(value, (ast.List, ast.Tuple)) and not value.elts
468
+
469
+
470
+ def _references(entity: EntityInfo, table: str) -> List[FieldInfo]:
471
+ """The foreign-key columns of ``entity`` that reference ``table``."""
472
+ return [
473
+ f
474
+ for f in entity.fields
475
+ # "schema.table.column": schema= is not read, the table name is matched.
476
+ if f.is_foreign_key
477
+ and (f.foreign_table or "").rpartition(".")[0].rpartition(".")[2] == table
478
+ ]
479
+
480
+
481
+ def _relationship_target(call: ast.Call) -> str | None:
482
+ """The class ``relationship(Target)`` points at: a name, ``"Target"`` or a lambda."""
483
+ target: ast.expr | None = call.args[0] if call.args else None
484
+ for kw in call.keywords:
485
+ if kw.arg == "argument":
486
+ target = kw.value
487
+ if isinstance(target, ast.Lambda):
488
+ target = target.body
489
+ if isinstance(target, ast.Constant) and isinstance(target.value, str):
490
+ return target.value.rpartition(".")[2]
491
+ if isinstance(target, (ast.Name, ast.Attribute)):
492
+ name = expr_name(target) # model.Customer -> Customer
493
+ return name.rpartition(".")[2] if name is not None else None
494
+ return None
495
+
496
+
497
+ def has_tablename(class_node: ast.ClassDef) -> bool:
498
+ """Check if a class assigns __tablename__ (annotated or plain)."""
499
+ for node in class_node.body:
500
+ if isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
501
+ if node.target.id == "__tablename__":
502
+ return True
503
+ elif isinstance(node, ast.Assign):
504
+ for target in node.targets:
505
+ if isinstance(target, ast.Name) and target.id == "__tablename__":
506
+ return True
507
+ return False
508
+
509
+
510
+ def has_mapped_field(class_node: ast.ClassDef) -> bool:
511
+ """Check if a class has at least one Mapped[...] annotated field."""
512
+ for node in class_node.body:
513
+ if isinstance(node, ast.AnnAssign) and is_mapped_annotation(node.annotation):
514
+ return True
515
+ return False
516
+
517
+
518
+ def core_table_name(call: ast.Call) -> str | None:
519
+ """The name of a Core ``Table("name", metadata, ...)``."""
520
+ if call.args and isinstance(call.args[0], ast.Constant) and isinstance(call.args[0].value, str):
521
+ return call.args[0].value
522
+ for kw in call.keywords:
523
+ if kw.arg == "name" and isinstance(kw.value, ast.Constant):
524
+ return str(kw.value.value)
525
+ return None
526
+
527
+
528
+ def build_core_table_entity(var_name: str, call: ast.Call) -> EntityInfo | None:
529
+ """An entity for a module-level Core ``Table(...)``, keyed by its variable.
530
+
531
+ None for a table without readable columns (e.g. a reflected
532
+ ``Table("t", metadata, autoload_with=engine)``). A table that is
533
+ structurally an association table is a link entity (#34).
534
+ """
535
+ fields = core_table_fields(call)
536
+ if not fields:
537
+ return None
538
+ entity = EntityInfo(
539
+ name=var_name,
540
+ table_name=core_table_name(call) or var_name,
541
+ base_classes=[],
542
+ source="sqlalchemy",
543
+ )
544
+ entity.fields = fields
545
+ entity.is_link_table = is_structural_link_table(fields)
546
+ return entity
547
+
548
+
549
+ def core_table_fields(call: ast.Call) -> List[FieldInfo]:
550
+ """The columns of a Core ``Table(...)``, with its table-level constraints applied.
551
+
552
+ Reads ``Column(...)`` arguments and single-column ``PrimaryKeyConstraint``,
553
+ ``ForeignKeyConstraint``, ``UniqueConstraint`` and unique ``Index`` ones.
554
+ """
555
+ fields: List[FieldInfo] = []
556
+ # Constraints name a column through its key (``key=``, else the name).
557
+ by_name: Dict[str, FieldInfo] = {}
558
+ for arg in call.args:
559
+ if is_call_to(arg, "Column") and isinstance(arg, ast.Call):
560
+ field = parse_core_column(arg)
561
+ if field is not None:
562
+ fields.append(field)
563
+ by_name[explicit_column_name(arg) or field.name] = field
564
+ for arg in call.args:
565
+ if not isinstance(arg, ast.Call):
566
+ continue
567
+ if is_call_to(arg, "PrimaryKeyConstraint"):
568
+ for name in _strings(arg.args):
569
+ if name in by_name:
570
+ by_name[name].is_primary_key = True
571
+ elif is_call_to(arg, "ForeignKeyConstraint") and len(arg.args) >= 2:
572
+ columns, targets = _strings_of(arg.args[0]), _strings_of(arg.args[1])
573
+ if len(columns) == 1 and len(targets) == 1 and columns[0] in by_name:
574
+ by_name[columns[0]].is_foreign_key = True
575
+ by_name[columns[0]].foreign_table = targets[0]
576
+ unique, _ = single_column_uniques(call.args)
577
+ for key in unique:
578
+ if key in by_name:
579
+ by_name[key].is_unique = True
580
+ for f in fields:
581
+ if f.is_primary_key:
582
+ f.is_nullable = False # a primary key is never NULL
583
+ f.is_unique = False
584
+ return fields
585
+
586
+
587
+ def parse_core_column(call: ast.Call) -> FieldInfo | None:
588
+ """Parse a Core ``Column("name", <Type>, ForeignKey(...), primary_key=...)``.
589
+
590
+ As in SQLAlchemy, a column is nullable unless it is a primary key or says
591
+ ``nullable=False``.
592
+ """
593
+ col_name = explicit_column_name(call, use_key=False)
594
+ if col_name is None:
595
+ return None
596
+ options = {kw.arg: kw.value for kw in call.keywords if kw.arg}
597
+
598
+ def flag(name: str) -> bool | None:
599
+ value = options.get(name)
600
+ if isinstance(value, ast.Constant) and isinstance(value.value, bool):
601
+ return value.value
602
+ return None
603
+
604
+ is_primary_key = bool(flag("primary_key"))
605
+ nullable = flag("nullable")
606
+ # An Identity() column is NOT NULL unless it says otherwise.
607
+ identity = any(is_call_to(arg, "Identity") for arg in call.args)
608
+ fk_target = extract_foreign_key_arg(call)
609
+
610
+ # The column type: type_=, else the first positional argument (after the
611
+ # name) that is not a schema item such as ForeignKey(...).
612
+ positional = (
613
+ call.args[1:] if call.args and isinstance(call.args[0], ast.Constant) else list(call.args)
614
+ )
615
+ candidates = [options["type_"]] if "type_" in options else positional
616
+ type_str = ""
617
+ for arg in candidates:
618
+ type_str = _core_type(arg)
619
+ if type_str:
620
+ break
621
+
622
+ default = options.get("default")
623
+ default_value = None
624
+ if isinstance(default, ast.Constant):
625
+ default_value = repr(default.value)
626
+ elif isinstance(default, ast.Attribute) and isinstance(default.value, ast.Name):
627
+ default_value = f"{default.value.id}.{default.attr}"
628
+
629
+ return FieldInfo(
630
+ name=col_name,
631
+ type_str=type_str,
632
+ is_primary_key=is_primary_key,
633
+ is_foreign_key=fk_target is not None,
634
+ is_nullable=(
635
+ not (is_primary_key or identity)
636
+ if nullable is None
637
+ else nullable and not is_primary_key
638
+ ),
639
+ foreign_table=fk_target,
640
+ index=bool(flag("index")),
641
+ default_value=default_value,
642
+ is_unique=bool(flag("unique")) and not is_primary_key,
643
+ )
644
+
645
+
646
+ def _core_type(arg: ast.expr) -> str:
647
+ """The Python type a ``Column()`` type argument maps to, as ORM columns show it.
648
+
649
+ ``Integer`` / ``String(50)`` -> ``int`` / ``str``, ``Enum(Status)`` ->
650
+ ``Status``; another type keeps its name. Empty for a schema item such as
651
+ ``ForeignKey(...)`` (not a type).
652
+ """
653
+ call = arg if isinstance(arg, ast.Call) else None
654
+ name = expr_name(call.func if call is not None else arg)
655
+ if name is None:
656
+ return ""
657
+ name = name.rpartition(".")[2]
658
+ if name in _SCHEMA_ITEMS:
659
+ return ""
660
+ if name == "Enum" and call is not None and call.args:
661
+ first = call.args[0]
662
+ if isinstance(first, ast.Constant) and isinstance(first.value, str):
663
+ return "str" # Enum("open", "closed", name="status")
664
+ enum_class = expr_name(first)
665
+ if enum_class is not None:
666
+ return enum_class.rpartition(".")[2]
667
+ options = {
668
+ kw.arg: kw.value.value
669
+ for kw in (call.keywords if call is not None else [])
670
+ if kw.arg and isinstance(kw.value, ast.Constant)
671
+ }
672
+ # Options that change the Python type: Uuid(as_uuid=False) holds strings,
673
+ # Numeric(asdecimal=False) floats and Float(asdecimal=True) Decimals.
674
+ if name in ("Uuid", "UUID") and options.get("as_uuid") is False:
675
+ return "str"
676
+ if "asdecimal" in options and name in _DECIMAL_TYPES:
677
+ return "Decimal" if options["asdecimal"] else "float"
678
+ return _PYTHON_TYPES.get(name, name)
679
+
680
+
681
+ #: Types whose ``asdecimal=`` option picks ``Decimal`` or ``float``.
682
+ _DECIMAL_TYPES = frozenset(
683
+ {
684
+ "Numeric",
685
+ "NUMERIC",
686
+ "DECIMAL",
687
+ "Float",
688
+ "Double",
689
+ "REAL",
690
+ "FLOAT",
691
+ "DOUBLE",
692
+ "DOUBLE_PRECISION",
693
+ }
694
+ )
695
+
696
+ #: SQLAlchemy column types -> the Python type of their values.
697
+ _PYTHON_TYPES = {
698
+ **dict.fromkeys(
699
+ ("Integer", "SmallInteger", "BigInteger", "INTEGER", "INT", "SMALLINT", "BIGINT"), "int"
700
+ ),
701
+ **dict.fromkeys(
702
+ (
703
+ "String",
704
+ "Text",
705
+ "Unicode",
706
+ "UnicodeText",
707
+ "VARCHAR",
708
+ "NVARCHAR",
709
+ "CHAR",
710
+ "NCHAR",
711
+ "TEXT",
712
+ "CLOB",
713
+ ),
714
+ "str",
715
+ ),
716
+ **dict.fromkeys(("Boolean", "BOOLEAN"), "bool"),
717
+ **dict.fromkeys(("Float", "Double", "REAL", "FLOAT", "DOUBLE", "DOUBLE_PRECISION"), "float"),
718
+ **dict.fromkeys(("Numeric", "NUMERIC", "DECIMAL"), "Decimal"),
719
+ **dict.fromkeys(("DateTime", "DATETIME", "TIMESTAMP"), "datetime"),
720
+ **dict.fromkeys(("Date", "DATE"), "date"),
721
+ **dict.fromkeys(("Time", "TIME"), "time"),
722
+ "Interval": "timedelta",
723
+ **dict.fromkeys(("LargeBinary", "BLOB", "BINARY", "VARBINARY"), "bytes"),
724
+ **dict.fromkeys(("Uuid", "UUID"), "UUID"),
725
+ **dict.fromkeys(("JSON", "JSONB"), "dict"),
726
+ }
727
+
728
+ #: Positional ``Column()`` arguments that are not its type.
729
+ _SCHEMA_ITEMS = frozenset(
730
+ {
731
+ "ForeignKey",
732
+ "Sequence",
733
+ "Identity",
734
+ "Computed",
735
+ "FetchedValue",
736
+ "DefaultClause",
737
+ "CheckConstraint",
738
+ }
739
+ )
740
+
741
+
742
+ def _strings(args: Iterable[ast.expr]) -> List[str]:
743
+ """The string constants among ``args``."""
744
+ return [a.value for a in args if isinstance(a, ast.Constant) and isinstance(a.value, str)]
745
+
746
+
747
+ def _strings_of(value: ast.expr) -> List[str]:
748
+ """The strings of a list / tuple literal (``["a", "b"]``)."""
749
+ return _strings(value.elts) if isinstance(value, (ast.List, ast.Tuple)) else []
750
+
751
+
752
+ def _links_to_name(node: ast.AST) -> bool:
753
+ """Whether a ``ForeignKey(..., link_to_name=True)`` appears in the declaration."""
754
+ return any(
755
+ isinstance(sub, ast.expr)
756
+ and is_call_to(sub, "ForeignKey")
757
+ and any(
758
+ kw.arg == "link_to_name"
759
+ and isinstance(kw.value, ast.Constant)
760
+ and kw.value.value is True
761
+ for kw in sub.keywords
762
+ )
763
+ for sub in ast.walk(node)
764
+ )
@@ -1,280 +0,0 @@
1
- """SQLModel and SQLAlchemy 2.0 (ORM classes and Core association tables)."""
2
-
3
- import ast
4
- from typing import Callable, Dict, Iterable, List, Mapping
5
-
6
- from ...config import EntityInfo, FieldInfo, is_structural_link_table
7
- from ..annotations import expr_name, is_call_to, is_mapped_annotation
8
- from .annotated import AnnotatedDialect, extract_foreign_key_arg
9
- from .base import ParseContext
10
-
11
-
12
- class TableDialect(AnnotatedDialect):
13
- """Shared by SQLModel and SQLAlchemy: classes mapped to tables.
14
-
15
- An explicit column name (``mapped_column("name")``, ``name=``,
16
- ``Field(sa_column=Column("name"))``, ``sa_column_kwargs={"name": ...}``)
17
- names the database column, so the column is drawn under it (#292).
18
- """
19
-
20
- def __init__(self, ctx: ParseContext):
21
- super().__init__(ctx)
22
- #: Table name -> {column key: column name} where an explicit ``key=``
23
- #: differs from the name (``mapped_column("account_id", key="id")``).
24
- self._renamed_keys: Dict[str, Dict[str, str]] = {}
25
- #: id() of foreign-key fields declared with ``link_to_name=True``: their
26
- #: target already names the column, not its key.
27
- # Keyed by id() but holding the object, so a freed object's address is
28
- # never matched by a new one.
29
- self._name_targets: Dict[int, FieldInfo] = {}
30
- #: id() of Core tables declared on ``SQLModel.metadata``: they belong
31
- #: to SQLModel's tables, although they are read as SQLAlchemy.
32
- self._sqlmodel_core: Dict[int, EntityInfo] = {}
33
-
34
- def _parse_field(self, node: ast.AnnAssign) -> FieldInfo | None:
35
- """Parse a column, remembering a ``ForeignKey(..., link_to_name=True)``."""
36
- field = super()._parse_field(node)
37
- if field is not None and field.is_foreign_key and _links_to_name(node):
38
- self._name_targets[id(field)] = field
39
- return field
40
-
41
- def finish(self, entity: EntityInfo, class_node: ast.ClassDef) -> None:
42
- """Mark ``__table_args__`` uniques, then rename columns to their explicit names."""
43
- # The unique constraints are matched by attribute name and key first.
44
- super().finish(entity, class_node)
45
- keys = self._db_column_names(class_node)
46
- names = self._db_column_names(class_node, use_key=False)
47
- renamed = {
48
- keys[attr]: name for attr, name in names.items() if attr in keys and keys[attr] != name
49
- }
50
- if renamed:
51
- self._renamed_keys[entity.table_name] = renamed
52
- for f in entity.fields:
53
- f.name = names.get(f.name, f.name)
54
-
55
- def finalize(self, entities: Mapping[str, EntityInfo]) -> None:
56
- """Point foreign keys written against a column key at the column's name.
57
-
58
- SQLAlchemy resolves ``ForeignKey("account.id")`` through the column
59
- key, so with ``mapped_column("account_id", key="id")`` it references
60
- ``account_id``.
61
- """
62
- # Only this dialect's tables: SQLModel and SQLAlchemy keep separate
63
- # metadata, so a table name may exist in both. A Core table belongs to
64
- # the metadata it is declared on.
65
- renamed = self._renamed_keys
66
- if not renamed:
67
- return
68
- sqlmodel_core: Dict[int, EntityInfo] = {}
69
- name_targets: Dict[int, FieldInfo] = {}
70
- for dialect in self.ctx.all_dialects():
71
- if isinstance(dialect, TableDialect):
72
- sqlmodel_core.update(dialect._sqlmodel_core)
73
- name_targets.update(dialect._name_targets)
74
- for entity in entities.values():
75
- kind = "sqlmodel" if sqlmodel_core.get(id(entity)) is entity else entity.source
76
- if kind != self.kind:
77
- continue
78
- for f in entity.fields:
79
- if f.foreign_table is None or name_targets.get(id(f)) is f:
80
- continue
81
- table, _, column = f.foreign_table.rpartition(".")
82
- name = renamed.get(table, {}).get(column)
83
- if name is not None:
84
- f.foreign_table = f"{table}.{name}"
85
-
86
-
87
- class SQLModelDialect(TableDialect):
88
- """SQLModel: a class declared with ``table=True``."""
89
-
90
- kind = "sqlmodel"
91
-
92
- def detect(self, class_node: ast.ClassDef) -> bool:
93
- for keyword in class_node.keywords:
94
- if keyword.arg == "table" and isinstance(keyword.value, ast.Constant):
95
- if keyword.value.value:
96
- return True
97
- return False
98
-
99
- def default_table_name(self, class_node: ast.ClassDef) -> str:
100
- """SQLModel's default ``__tablename__``: the lower-cased class name (#291).
101
-
102
- ``HeroTeam`` -> ``heroteam``, not snake_case; foreign keys written as
103
- ``foreign_key="heroteam.id"`` must find it.
104
- """
105
- return class_node.name.lower()
106
-
107
-
108
- class SQLAlchemyDialect(TableDialect):
109
- """SQLAlchemy 2.0: a concrete mapped class has ``__tablename__`` and ``Mapped[...]``.
110
-
111
- Mixins / abstract bases (``Mapped`` fields but no ``__tablename__``) are not
112
- detected, so they are not emitted as entities, but their fields are still
113
- inherited.
114
- """
115
-
116
- kind = "sqlalchemy"
117
- reads_annotated_columns = True
118
- plain_value_is_column = False
119
-
120
- def detect(self, class_node: ast.ClassDef) -> bool:
121
- return has_tablename(class_node) and has_mapped_field(class_node)
122
-
123
- def extra_entities(self, trees: Iterable[ast.Module]) -> List[EntityInfo]:
124
- """Core association tables (:func:`core_association_tables`, #34)."""
125
- entities = core_association_tables(trees, self._note_core_table)
126
- return entities
127
-
128
- def _is_sqlmodel_metadata(self, metadata: ast.expr) -> bool:
129
- """Whether ``metadata`` is ``SQLModel.metadata``, also through a scanned subclass."""
130
- if not (isinstance(metadata, ast.Attribute) and metadata.attr == "metadata"):
131
- return False
132
- owner = expr_name(metadata.value)
133
- if owner == "SQLModel":
134
- return True
135
- node = self.classes.all.get(owner) if owner is not None else None
136
- return node is not None and any(
137
- expr_name(base) == "SQLModel"
138
- for name in self.classes.dfs_order(node, set())
139
- for base in self.classes.all[name].bases
140
- )
141
-
142
- def _note_core_table(self, entity: EntityInfo, call: ast.Call) -> None:
143
- """Record what the key rewrite needs: the metadata, ``link_to_name`` columns."""
144
- if len(call.args) > 1 and self._is_sqlmodel_metadata(call.args[1]):
145
- self._sqlmodel_core[id(entity)] = entity
146
- columns = {f.name: f for f in entity.fields}
147
- for arg in call.args:
148
- if is_call_to(arg, "Column") and arg.args and isinstance(arg.args[0], ast.Constant):
149
- field = columns.get(str(arg.args[0].value))
150
- if field is not None and _links_to_name(arg):
151
- self._name_targets[id(field)] = field
152
-
153
-
154
- def core_association_tables(
155
- trees: Iterable[ast.Module],
156
- on_table: Callable[[EntityInfo, ast.Call], None] | None = None,
157
- ) -> List[EntityInfo]:
158
- """Link entities from module-level Core ``Table(...)`` assignments.
159
-
160
- ``relationship(secondary=Table(...))`` points at a SQLAlchemy Core table
161
- rather than a mapped class. Only class definitions become entities in the
162
- normal passes, so such association tables - and their M:N - would be lost.
163
- Parse module-level ``name = Table("t", metadata, Column(...), ...)``
164
- assignments that are structurally association tables (a composite PK
165
- made up entirely of FK columns) and return them as link entities (#34).
166
- ``on_table`` is called with each entity and its ``Table(...)`` call.
167
- """
168
- entities: List[EntityInfo] = []
169
- for tree in trees:
170
- for node in tree.body:
171
- if not isinstance(node, ast.Assign):
172
- continue
173
- if len(node.targets) != 1 or not isinstance(node.targets[0], ast.Name):
174
- continue
175
- if not is_call_to(node.value, "Table"):
176
- continue
177
-
178
- entity = build_core_table_entity(node.targets[0].id, node.value)
179
- if entity is not None:
180
- if on_table is not None:
181
- on_table(entity, node.value)
182
- entities.append(entity)
183
- return entities
184
-
185
-
186
- def has_tablename(class_node: ast.ClassDef) -> bool:
187
- """Check if a class assigns __tablename__ (annotated or plain)."""
188
- for node in class_node.body:
189
- if isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
190
- if node.target.id == "__tablename__":
191
- return True
192
- elif isinstance(node, ast.Assign):
193
- for target in node.targets:
194
- if isinstance(target, ast.Name) and target.id == "__tablename__":
195
- return True
196
- return False
197
-
198
-
199
- def has_mapped_field(class_node: ast.ClassDef) -> bool:
200
- """Check if a class has at least one Mapped[...] annotated field."""
201
- for node in class_node.body:
202
- if isinstance(node, ast.AnnAssign) and is_mapped_annotation(node.annotation):
203
- return True
204
- return False
205
-
206
-
207
- def build_core_table_entity(var_name: str, call: ast.Call) -> EntityInfo | None:
208
- """Build a link EntityInfo from a Core ``Table(...)`` call, or None if it
209
- is not structurally an association table."""
210
- # Table name is the first positional string arg; fall back to variable name.
211
- table_name = var_name
212
- if call.args and isinstance(call.args[0], ast.Constant) and isinstance(call.args[0].value, str):
213
- table_name = call.args[0].value
214
-
215
- fields: List[FieldInfo] = []
216
- for arg in call.args:
217
- if is_call_to(arg, "Column"):
218
- field = parse_core_column(arg)
219
- if field is not None:
220
- fields.append(field)
221
-
222
- if not is_structural_link_table(fields):
223
- return None
224
-
225
- entity = EntityInfo(
226
- name=var_name,
227
- table_name=table_name,
228
- base_classes=[],
229
- source="sqlalchemy",
230
- )
231
- entity.fields = fields
232
- entity.is_link_table = True
233
- return entity
234
-
235
-
236
- def parse_core_column(call: ast.Call) -> FieldInfo | None:
237
- """Parse a Core ``Column("name", <Type>, ForeignKey(...), primary_key=...)``."""
238
- if not call.args or not isinstance(call.args[0], ast.Constant):
239
- return None
240
- col_name = str(call.args[0].value)
241
-
242
- is_primary_key = False
243
- for keyword in call.keywords:
244
- if keyword.arg == "primary_key" and isinstance(keyword.value, ast.Constant):
245
- is_primary_key = bool(keyword.value.value)
246
-
247
- fk_target = extract_foreign_key_arg(call)
248
-
249
- # An optional positional column type (e.g. Column("id", Integer, ...)).
250
- type_str = ""
251
- for arg in call.args[1:]:
252
- if isinstance(arg, ast.Name):
253
- type_str = arg.id
254
- break
255
- if isinstance(arg, ast.Attribute):
256
- type_str = arg.attr
257
- break
258
-
259
- return FieldInfo(
260
- name=col_name,
261
- type_str=type_str,
262
- is_primary_key=is_primary_key,
263
- is_foreign_key=fk_target is not None,
264
- foreign_table=fk_target,
265
- )
266
-
267
-
268
- def _links_to_name(node: ast.AST) -> bool:
269
- """Whether a ``ForeignKey(..., link_to_name=True)`` appears in the declaration."""
270
- return any(
271
- isinstance(sub, ast.expr)
272
- and is_call_to(sub, "ForeignKey")
273
- and any(
274
- kw.arg == "link_to_name"
275
- and isinstance(kw.value, ast.Constant)
276
- and kw.value.value is True
277
- for kw in sub.keywords
278
- )
279
- for sub in ast.walk(node)
280
- )
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes