Skip to content

Commit

Permalink
Optimize columns loading: lazy create row locator
Browse files Browse the repository at this point in the history
  • Loading branch information
Reskov committed Oct 10, 2021
1 parent 968e8da commit 227e035
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/gino/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ def __init__(self, instance: "CRUDModel"):
self._props = {}
self._literal = True
self._locator = None
if instance.__table__ is not None:

@property
def locator(self):
if self._locator is None and self._instance.__table__ is not None:
try:
self._locator = instance.lookup()
self._locator = self._instance.lookup()
except LookupError:
# apply() will fail anyway, but still allow update()
pass

return self._locator

def _set(self, key, value):
self._values[key] = value

Expand All @@ -116,7 +121,7 @@ async def apply(self, bind=None, timeout=DEFAULT):
:return: ``self`` for chaining calls.
"""
if self._locator is None:
if self.locator is None:
raise TypeError(
"Model {} has no table, primary key or custom lookup()".format(
self._instance.__class__.__name__
Expand Down Expand Up @@ -167,7 +172,7 @@ async def apply(self, bind=None, timeout=DEFAULT):
clause = (
type(self._instance)
.update.where(
self._locator,
self.locator,
)
.values(
**self._instance._get_sa_values(values),
Expand Down Expand Up @@ -435,7 +440,8 @@ class CRUDModel(Model):
def __init__(self, **values):
super().__init__()
self.__profile__ = None
self._update_request_cls(self).update(**values)
if values:
self._update_request_cls(self).update(**values)

@classmethod
def _init_table(cls, sub_cls):
Expand Down

0 comments on commit 227e035

Please sign in to comment.