Skip to content

Commit 9d99175

Browse files
authored
Merge pull request #437 from reagento/pr-408
Fixed #408
2 parents a6dafb6 + a18eb12 commit 9d99175

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix introspection of SQLAlchemy models with custom column name

src/adaptix/_internal/model_tools/introspection/sqlalchemy.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ def _get_input_shape(
131131
autoincrement_column = _get_autoincrement_column(table)
132132
fields = []
133133
params = []
134-
for column in columns:
134+
for field_name, column in columns.items():
135135
if not isinstance(column, sqlalchemy.Column):
136136
continue
137137

138138
fields.append(
139139
InputField(
140-
id=column.key,
140+
id=field_name,
141141
type=_get_type_for_column(column, type_hints),
142142
default=_get_default(column.default),
143143
is_required=_is_input_required_for_column(column, autoincrement_column),
@@ -147,8 +147,8 @@ def _get_input_shape(
147147
)
148148
params.append(
149149
Param(
150-
field_id=column.key,
151-
name=column.key,
150+
field_id=field_name,
151+
name=field_name,
152152
kind=ParamKind.KW_ONLY,
153153
),
154154
)
@@ -193,14 +193,14 @@ def _get_output_shape(
193193
) -> OutputShape:
194194
output_fields = [
195195
OutputField(
196-
id=column.name,
196+
id=field_name,
197197
type=_get_type_for_column(column, type_hints),
198198
default=_get_default(column.default),
199199
metadata=column.info,
200200
original=IdWrapper(column),
201-
accessor=create_attr_accessor(column.name, is_required=True),
201+
accessor=create_attr_accessor(field_name, is_required=True),
202202
)
203-
for column in columns
203+
for field_name, column in columns.items()
204204
if isinstance(column, sqlalchemy.Column)
205205
]
206206
for relationship in relationships:

tests/integration/morphing/test_sqlalchemy.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,63 @@ class Declarative2:
135135
==
136136
{"id": 1, "text": "abc", "children": [{"id": 2, "parent_id": None}]}
137137
)
138+
139+
140+
def test_o2o_relationship_with_custom_name_pos(accum):
141+
mapper_registry = registry()
142+
143+
@mapper_registry.mapped
144+
class Declarative1:
145+
__tablename__ = "DeclarativeModel1"
146+
147+
id: Mapped[int] = mapped_column("Id", primary_key=True)
148+
149+
__eq__ = sqlalchemy_equals
150+
151+
@mapper_registry.mapped
152+
class Declarative2:
153+
__tablename__ = "DeclarativeModel2"
154+
155+
id: Mapped[int] = mapped_column("Id", primary_key=True)
156+
text: Mapped[str] = mapped_column("Text")
157+
158+
parent_id: Mapped[Optional[int]] = mapped_column("ParentId", ForeignKey(Declarative1.id))
159+
parent: Mapped[Optional[Declarative1]] = relationship()
160+
161+
__eq__ = sqlalchemy_equals
162+
163+
retort = Retort(recipe=[accum])
164+
165+
loader = retort.get_loader(Declarative2)
166+
assert (
167+
loader({"id": 1, "text": "abc", "parent_id": 100})
168+
==
169+
Declarative2(id=1, text="abc", parent_id=100)
170+
)
171+
assert (
172+
loader({"id": 1, "text": "abc", "parent": {"id": 100}})
173+
==
174+
Declarative2(id=1, text="abc", parent=Declarative1(id=100))
175+
)
176+
assert (
177+
loader({"id": 1, "text": "abc", "parent_id": 100, "parent": {"id": 100}})
178+
==
179+
Declarative2(id=1, text="abc", parent_id=100, parent=Declarative1(id=100))
180+
)
181+
182+
dumper = retort.get_dumper(Declarative2)
183+
assert (
184+
dumper(Declarative2(id=1, text="abc", parent_id=100))
185+
==
186+
{"id": 1, "text": "abc", "parent_id": 100, "parent": None}
187+
)
188+
assert (
189+
dumper(Declarative2(id=1, text="abc", parent=Declarative1(id=100)))
190+
==
191+
{"id": 1, "text": "abc", "parent_id": None, "parent": {"id": 100}}
192+
)
193+
assert (
194+
dumper(Declarative2(id=1, text="abc", parent_id=100, parent=Declarative1(id=100)))
195+
==
196+
{"id": 1, "text": "abc", "parent_id": 100, "parent": {"id": 100}}
197+
)

0 commit comments

Comments
 (0)