From 64e98f9f0a7d60ec07797b2a823a4cdccd64ea92 Mon Sep 17 00:00:00 2001 From: Brian Wiborg Date: Thu, 2 Oct 2025 14:30:53 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20Automatically=20prefix?= =?UTF-8?q?=20table=5Fnames=20with=20app=5Flabel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ohmyapi/db/model/model.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/ohmyapi/db/model/model.py b/src/ohmyapi/db/model/model.py index 929ff60..642603d 100644 --- a/src/ohmyapi/db/model/model.py +++ b/src/ohmyapi/db/model/model.py @@ -34,6 +34,23 @@ class ModelMeta(type(TortoiseModel)): # Grab the Schema class for further processing. schema_opts = attrs.get("Schema", None) + # Create or get the inner Meta class + meta = attrs.get("Meta", type("Meta", (), {})) + + # Infer app name from module if not explicitly set + if not hasattr(meta, "app"): + module = attrs.get("__module__", "") + module_parts = module.split(".") + if module_parts[-1] == "models": + inferred_app = module_parts[-2] + else: + inferred_app = module_parts[-1] + meta.app = inferred_app + + # Prefix table name if not explicitly set + if not hasattr(meta, "table"): + meta.table = f"{meta.app}_{name.lower()}" + # Let Tortoise's Metaclass do it's thing. new_cls = super().__new__(mcls, name, bases, attrs)