-
-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
table_name_generator
attribute to Meta for dynamic table name g…
…eneration (#1770) * add table_name_generator attribute to Meta * add changelog * fix typing hints * change combinision behaviour * change to glabal table name generator * remove extra lines * remove print * add annotation for example
- Loading branch information
1 parent
49b36ad
commit 4532f2d
Showing
4 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
This example demonstrates how to use the global table name generator to automatically | ||
generate snake_case table names for all models, and how explicit table names take precedence. | ||
""" | ||
|
||
from tortoise import Tortoise, fields, run_async | ||
from tortoise.models import Model | ||
|
||
|
||
def snake_case_table_names(cls): | ||
"""Convert CamelCase class name to snake_case table name""" | ||
name = cls.__name__ | ||
return "".join(["_" + c.lower() if c.isupper() else c for c in name]).lstrip("_") | ||
|
||
|
||
class UserProfile(Model): | ||
id = fields.IntField(primary_key=True) | ||
name = fields.TextField() | ||
created_at = fields.DatetimeField(auto_now_add=True) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
|
||
class BlogPost(Model): | ||
id = fields.IntField(primary_key=True) | ||
title = fields.TextField() | ||
author: fields.ForeignKeyRelation[UserProfile] = fields.ForeignKeyField( | ||
"models.UserProfile", related_name="posts" | ||
) | ||
|
||
class Meta: | ||
table = "custom_blog_posts" | ||
|
||
def __str__(self): | ||
return self.title | ||
|
||
|
||
async def run(): | ||
# Initialize with snake_case table name generator | ||
await Tortoise.init( | ||
db_url="sqlite://:memory:", | ||
modules={"models": ["__main__"]}, | ||
table_name_generator=snake_case_table_names, | ||
) | ||
await Tortoise.generate_schemas() | ||
|
||
# UserProfile uses generated name, BlogPost uses explicit table name | ||
print(f"UserProfile table name: {UserProfile._meta.db_table}") # >>> user_profile | ||
print(f"BlogPost table name: {BlogPost._meta.db_table}") # >>> custom_blog_posts | ||
|
||
await Tortoise.close_connections() | ||
|
||
|
||
if __name__ == "__main__": | ||
run_async(run()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Type | ||
|
||
from tortoise import Tortoise, fields | ||
from tortoise.contrib.test import SimpleTestCase | ||
from tortoise.models import Model | ||
|
||
|
||
def table_name_generator(model_cls: Type[Model]): | ||
return f"test_{model_cls.__name__.lower()}" | ||
|
||
|
||
class Tournament(Model): | ||
id = fields.IntField(pk=True) | ||
name = fields.TextField() | ||
created_at = fields.DatetimeField(auto_now_add=True) | ||
|
||
|
||
class CustomTable(Model): | ||
id = fields.IntField(pk=True) | ||
name = fields.TextField() | ||
|
||
class Meta: | ||
table = "my_custom_table" | ||
|
||
|
||
class TestTableNameGenerator(SimpleTestCase): | ||
async def asyncSetUp(self): | ||
await super().asyncSetUp() | ||
await Tortoise.init( | ||
db_url="sqlite://:memory:", | ||
modules={"models": [__name__]}, | ||
table_name_generator=table_name_generator, | ||
) | ||
await Tortoise.generate_schemas() | ||
|
||
async def asyncTearDown(self): | ||
await Tortoise.close_connections() | ||
|
||
async def test_glabal_name_generator(self): | ||
self.assertEqual(Tournament._meta.db_table, "test_tournament") | ||
|
||
async def test_custom_table_name_precedence(self): | ||
self.assertEqual(CustomTable._meta.db_table, "my_custom_table") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters