-
-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathmodels_schema_create.py
110 lines (79 loc) · 3.26 KB
/
models_schema_create.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
This example demonstrates SQL Schema generation for each DB type supported.
"""
from tortoise import fields
from tortoise.models import Model
class Tournament(Model):
tid = fields.SmallIntField(pk=True)
name = fields.CharField(max_length=100, description="Tournament name", index=True)
created = fields.DatetimeField(auto_now_add=True, description="Created */'`/* datetime")
class Meta:
table_description = "What Tournaments */'`/* we have"
class Event(Model):
id = fields.BigIntField(pk=True, description="Event ID")
name = fields.TextField()
tournament = fields.ForeignKeyField(
"models.Tournament", related_name="events", description="FK to tournament"
)
participants = fields.ManyToManyField(
"models.Team",
related_name="events",
through="teamevents",
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)
key = fields.CharField(max_length=100)
class Meta:
table_description = "This table contains a list of all the events"
unique_together = [("name", "prize"), ["tournament", "key"]]
class Team(Model):
name = fields.CharField(max_length=50, pk=True, description="The TEAM name (and PK)")
key = fields.IntField()
manager = fields.ForeignKeyField("models.Team", related_name="team_members", null=True)
talks_to = fields.ManyToManyField("models.Team", related_name="gets_talked_to")
class Meta:
table_description = "The TEAMS!"
indexes = [("manager", "key"), ["manager_id", "name"]]
class TeamAddress(Model):
city = fields.CharField(max_length=50, description="City")
country = fields.CharField(max_length=50, description="Country")
street = fields.CharField(max_length=128, description="Street Address")
team = fields.OneToOneField(
"models.Team", related_name="address", on_delete=fields.CASCADE, pk=True
)
class VenueInformation(Model):
name = fields.CharField(max_length=128)
capacity = fields.IntField()
rent = fields.FloatField()
team = fields.OneToOneField("models.Team", on_delete=fields.SET_NULL, null=True)
class SourceFields(Model):
id = fields.IntField(pk=True, source_field="sometable_id")
chars = fields.CharField(max_length=255, source_field="some_chars_table", index=True)
fk = fields.ForeignKeyField(
"models.SourceFields", related_name="team_members", null=True, source_field="fk_sometable"
)
rel_to = fields.ManyToManyField(
"models.SourceFields",
related_name="rel_from",
through="sometable_self",
forward_key="sts_forward",
backward_key="backward_sts",
)
class Meta:
table = "sometable"
class DefaultPK(Model):
val = fields.IntField()
class ZeroMixin:
zero = fields.IntField()
class OneMixin(ZeroMixin):
one = fields.CharField(40, null=True)
class TwoMixin:
two = fields.CharField(40)
class AbstractModel(Model, OneMixin):
new_field = fields.CharField(max_length=100)
class Meta:
abstract = True
class InheritedModel(AbstractModel, TwoMixin):
name = fields.TextField()