preliminary release
This commit is contained in:
@@ -55,7 +55,12 @@ def dependency_rank(target_key: str) -> int:
|
||||
class LinkResolver:
|
||||
"""Looks up parent primary keys by natural key within the batch's session,
|
||||
so parents written earlier in the same uncommitted transaction are found.
|
||||
A missing parent leaves the FK null, which Core allows."""
|
||||
|
||||
The natural key wins over an id the source sent: a hospital table carries
|
||||
the hospital's internal ids, which only match ours when the parent was
|
||||
loaded with its own id. An id is kept as sent only when there is no
|
||||
natural key or its parent isn't loaded; a missing parent leaves the FK
|
||||
null, which Core allows."""
|
||||
|
||||
def __init__(self, session: Session):
|
||||
self._session = session
|
||||
@@ -65,12 +70,13 @@ class LinkResolver:
|
||||
linked = []
|
||||
for link in links_for(target_key):
|
||||
value = row.get(link.via_field)
|
||||
if row.get(link.fk_field) is not None or value is None:
|
||||
if value is None:
|
||||
continue
|
||||
pk = self._lookup(link, value)
|
||||
if pk is not None:
|
||||
if row.get(link.fk_field) != pk:
|
||||
linked.append(link.fk_field)
|
||||
row[link.fk_field] = pk
|
||||
linked.append(link.fk_field)
|
||||
return linked
|
||||
|
||||
def _lookup(self, link: ForeignKeyLink, value: Any) -> Any:
|
||||
|
||||
Reference in New Issue
Block a user