Skip to content

Commit 21a7e8d

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

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

advanced_alchemy/repository/_async.py

Lines changed: 10 additions & 10 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 (new_value := getattr(data, relationship.key, MISSING)) is not MISSING:
1514-
# 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
1513+
# Skip relationships that cannot be handled by generic merge operations
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
15231522

1523+
if (new_value := getattr(data, relationship.key, MISSING)) is not MISSING:
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: 10 additions & 10 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 (new_value := getattr(data, relationship.key, MISSING)) is not MISSING:
1515-
# 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
1514+
# 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
15241523

1524+
if (new_value := getattr(data, relationship.key, MISSING)) is not MISSING:
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)