🎨 Auto-prefix table_names with "{app_label}_"

There is no support for auto-prefixing implicit proxy-tables in
Tortoise. If you need to prefix a proxy-table, explicitly define the
Model for the proxy table. Being an explicit model, it will then be
auto-prefixed.
This commit is contained in:
Brian Wiborg 2025-10-11 13:03:31 +02:00
parent acd4844a25
commit ce47e3f60e
No known key found for this signature in database

View file

@ -31,6 +31,18 @@ UUID.__get_pydantic_core_schema__ = classmethod(__uuid_schema_monkey_patch)
class ModelMeta(type(TortoiseModel)): class ModelMeta(type(TortoiseModel)):
def __new__(mcls, name, bases, attrs): def __new__(mcls, name, bases, attrs):
meta = attrs.get("Meta", None)
if meta is None:
class Meta:
pass
meta = Meta
attrs["Meta"] = meta
if not hasattr(meta, "table"):
# Use first part of module as app_label
app_label = attrs.get("__module__", "").replace("ohmyapi.builtin.", "ohmyapi_").split(".")[0]
setattr(meta, "table", f"{app_label}_{name.lower()}")
# Grab the Schema class for further processing. # Grab the Schema class for further processing.
schema_opts = attrs.get("Schema", None) schema_opts = attrs.get("Schema", None)