-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
45 lines (34 loc) · 1.46 KB
/
models.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
# from app import db
from database import Base
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import backref, relationship
# Create our classes, one for each table
# One to many - Each player belongs to 1 team. Each team has many players
# One to one - Each team has 1 captain. Each captain can only belong to 1 team.
class Team(Base):
__tablename__ = 'teams'
id = Column(Integer, primary_key=True)
name = Column(String(50))
rank = Column(Integer, nullable=False)
players = relationship('Player', backref='on_team', lazy=True)
def __repr__(self):
return '<Team %r>' % self.name
class Player(Base):
__tablename__ = 'players'
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
position = Column(String(50), nullable=False)
year = Column(Integer, nullable=False)
team_id = Column(Integer, ForeignKey('teams.id'), nullable=False)
def __repr__(self):
return '<Player %r>' % self.name
class Game(Base):
__tablename__ = 'games'
id = Column(Integer, primary_key=True)
level = Column(String(30), nullable=False)
child_id = Column(Integer, ForeignKey('games.id'), nullable=True)
winner_id = Column(Integer, ForeignKey('teams.id'), nullable=False)
loser_id = Column(Integer, ForeignKey('teams.id'), nullable=False)
child = relationship('Game', remote_side=[id])
def __repr__(self):
return '<Game %r>' % self.winner_id