-
Notifications
You must be signed in to change notification settings - Fork 500
Expand file tree
/
Copy pathschema_create.py
More file actions
73 lines (55 loc) · 2.33 KB
/
Copy pathschema_create.py
File metadata and controls
73 lines (55 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
This example demonstrates SQL Schema generation for each DB type supported.
"""
from tortoise import Tortoise, connections, fields, run_async
from tortoise.models import Model
from tortoise.utils import get_schema_sql
class Tournament(Model):
id = fields.IntField(primary_key=True)
name = fields.CharField(max_length=255, description="Tournament name", db_index=True)
created = fields.DatetimeField(auto_now_add=True, description="Created datetime")
events: fields.ReverseRelation["Event"]
class Meta:
table_description = "What Tournaments we have"
class Event(Model):
id = fields.IntField(primary_key=True, description="Event ID")
name = fields.CharField(max_length=255, unique=True)
tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKeyField(
"models.Tournament",
related_name="events",
description="FK to tournament",
on_delete=fields.RESTRICT,
)
participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField(
"models.Team",
related_name="events",
through="event_team",
description="How participants relate",
)
modified = fields.DatetimeField(auto_now=True)
prize = fields.DecimalField(max_digits=10, decimal_places=2, null=True)
token = fields.CharField(max_length=100, description="Unique token", unique=True)
class Meta:
table_description = "This table contains a list of all the events"
class Team(Model):
name = fields.CharField(max_length=50, primary_key=True, description="The TEAM name (and PK)")
events: fields.ManyToManyRelation[Event]
class Meta:
table_description = "The TEAMS!"
async def run():
print("SQLite:\n")
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
sql = get_schema_sql(connections.get("default"), safe=False)
print(sql)
print("\n\nMySQL:\n")
await Tortoise.init(db_url="mysql://root:@127.0.0.1:3306/", modules={"models": ["__main__"]})
sql = get_schema_sql(connections.get("default"), safe=False)
print(sql)
print("\n\nPostgreSQL:\n")
await Tortoise.init(
db_url="postgres://postgres:@127.0.0.1:5432/", modules={"models": ["__main__"]}
)
sql = get_schema_sql(connections.get("default"), safe=False)
print(sql)
if __name__ == "__main__":
run_async(run())