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)