Skip to content

Commit 227e035

Browse files
committed
Optimize columns loading: lazy create row locator
1 parent 968e8da commit 227e035

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/gino/crud.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,18 @@ def __init__(self, instance: "CRUDModel"):
8787
self._props = {}
8888
self._literal = True
8989
self._locator = None
90-
if instance.__table__ is not None:
90+
91+
@property
92+
def locator(self):
93+
if self._locator is None and self._instance.__table__ is not None:
9194
try:
92-
self._locator = instance.lookup()
95+
self._locator = self._instance.lookup()
9396
except LookupError:
9497
# apply() will fail anyway, but still allow update()
9598
pass
9699

100+
return self._locator
101+
97102
def _set(self, key, value):
98103
self._values[key] = value
99104

@@ -116,7 +121,7 @@ async def apply(self, bind=None, timeout=DEFAULT):
116121
:return: ``self`` for chaining calls.
117122
118123
"""
119-
if self._locator is None:
124+
if self.locator is None:
120125
raise TypeError(
121126
"Model {} has no table, primary key or custom lookup()".format(
122127
self._instance.__class__.__name__
@@ -167,7 +172,7 @@ async def apply(self, bind=None, timeout=DEFAULT):
167172
clause = (
168173
type(self._instance)
169174
.update.where(
170-
self._locator,
175+
self.locator,
171176
)
172177
.values(
173178
**self._instance._get_sa_values(values),
@@ -435,7 +440,8 @@ class CRUDModel(Model):
435440
def __init__(self, **values):
436441
super().__init__()
437442
self.__profile__ = None
438-
self._update_request_cls(self).update(**values)
443+
if values:
444+
self._update_request_cls(self).update(**values)
439445

440446
@classmethod
441447
def _init_table(cls, sub_cls):

0 commit comments

Comments
 (0)