Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit a364e43

Browse files
authored
Add metadata property (#142)
1 parent c6b9c0e commit a364e43

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

orm/models.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ class ModelRegistry:
2525
def __init__(self, database: databases.Database) -> None:
2626
self.database = database
2727
self.models = {}
28-
self.metadata = sqlalchemy.MetaData()
28+
self._metadata = sqlalchemy.MetaData()
29+
30+
@property
31+
def metadata(self):
32+
for model_cls in self.models.values():
33+
model_cls.build_table()
34+
return self._metadata
2935

3036
async def create_all(self):
3137
url = self._get_database_url()
3238
engine = create_async_engine(url)
3339

34-
for model_cls in self.models.values():
35-
model_cls.build_table()
36-
3740
async with self.database:
3841
async with engine.begin() as conn:
3942
await conn.run_sync(self.metadata.create_all)
@@ -44,9 +47,6 @@ async def drop_all(self):
4447
url = self._get_database_url()
4548
engine = create_async_engine(url)
4649

47-
for model_cls in self.models.values():
48-
model_cls.build_table()
49-
5050
async with self.database:
5151
async with engine.begin() as conn:
5252
await conn.run_sync(self.metadata.drop_all)
@@ -498,7 +498,7 @@ def __str__(self):
498498
@classmethod
499499
def build_table(cls):
500500
tablename = cls.tablename
501-
metadata = cls.registry.metadata
501+
metadata = cls.registry._metadata
502502
columns = []
503503
for name, field in cls.fields.items():
504504
columns.append(field.get_column(name))

0 commit comments

Comments
 (0)