Skip to content

Commit 6ce7291

Browse files
committed
fix: accessing potentially lazy attributes before check on update
1 parent a217d2c commit 6ce7291

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

advanced_alchemy/repository/_async.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,17 +1510,17 @@ async def update(
15101510

15111511
# Handle relationships by merging objects into session first
15121512
for relationship in mapper.mapper.relationships:
1513+
if relationship.viewonly or relationship.lazy in { # pragma: no cover
1514+
"write_only",
1515+
"dynamic",
1516+
"raise",
1517+
"raise_on_sql",
1518+
}:
1519+
# Skip relationships with incompatible lazy loading strategies
1520+
continue
1521+
15131522
if (new_value := getattr(data, relationship.key, MISSING)) is not MISSING:
15141523
# Skip relationships that cannot be handled by generic merge operations
1515-
if relationship.viewonly or relationship.lazy in { # pragma: no cover
1516-
"write_only",
1517-
"dynamic",
1518-
"raise",
1519-
"raise_on_sql",
1520-
}:
1521-
# Skip relationships with incompatible lazy loading strategies
1522-
continue
1523-
15241524
if isinstance(new_value, list):
15251525
merged_values = [ # pyright: ignore
15261526
await self.session.merge(item, load=False) # pyright: ignore

advanced_alchemy/repository/_sync.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,17 +1511,17 @@ def update(
15111511

15121512
# Handle relationships by merging objects into session first
15131513
for relationship in mapper.mapper.relationships:
1514+
if relationship.viewonly or relationship.lazy in { # pragma: no cover
1515+
"write_only",
1516+
"dynamic",
1517+
"raise",
1518+
"raise_on_sql",
1519+
}:
1520+
# Skip relationships with incompatible lazy loading strategies
1521+
continue
1522+
15141523
if (new_value := getattr(data, relationship.key, MISSING)) is not MISSING:
15151524
# Skip relationships that cannot be handled by generic merge operations
1516-
if relationship.viewonly or relationship.lazy in { # pragma: no cover
1517-
"write_only",
1518-
"dynamic",
1519-
"raise",
1520-
"raise_on_sql",
1521-
}:
1522-
# Skip relationships with incompatible lazy loading strategies
1523-
continue
1524-
15251525
if isinstance(new_value, list):
15261526
merged_values = [ # pyright: ignore
15271527
self.session.merge(item, load=False) # pyright: ignore

0 commit comments

Comments
 (0)