debug one to many relationships

This commit is contained in:
Sorrel Bri 2019-10-10 17:41:49 -07:00
parent 3229ab0f96
commit 76b2afe94e
9 changed files with 71 additions and 92 deletions

View file

@ -1,7 +1,6 @@
if __name__ == '__main__':
from ..models.User import User
from ..models.GameRoom import GameRoom
from ..models.TimeSettings import TimeSettings
from ..models.Game import Game
from ..models.Move import Move
from ..models.Message import Message

View file

@ -15,7 +15,6 @@ from models.Game import Game
from models.GameRoom import GameRoom
from models.Message import Message
from models.Move import Move
from models.TimeSettings import TimeSettings
from models.User import User
# migrations

View file

@ -1,8 +1,8 @@
"""empty message
Revision ID: acabfc6d158d
Revision ID: b077aaee9ec8
Revises:
Create Date: 2019-10-10 14:25:27.198731
Create Date: 2019-10-10 17:38:14.900202
"""
from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'acabfc6d158d'
revision = 'b077aaee9ec8'
down_revision = None
branch_labels = None
depends_on = None
@ -18,20 +18,12 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('room_languages',
op.create_table('game_rooms',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=40), nullable=False),
sa.Column('iso', sa.String(length=2), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('time_settings',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('main_time', sa.Enum('BYOYOMI', 'ABSOLUTE', 'HOURGLASS', 'NONE', name='timetypes'), nullable=False),
sa.Column('time_period', sa.Integer(), nullable=True),
sa.Column('period_length', sa.Integer(), nullable=True),
sa.Column('overtime', sa.Enum('BYOYOMI', 'ABSOLUTE', 'HOURGLASS', 'NONE', name='timetypes'), nullable=False),
sa.Column('overtime_period', sa.Integer(), nullable=True),
sa.Column('overtime_length', sa.Integer(), nullable=True),
sa.Column('description', sa.String(length=200), nullable=False),
sa.Column('private', sa.Boolean(), nullable=False),
sa.Column('language', sa.Enum('EN', name='languages'), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('users',
@ -48,15 +40,6 @@ def upgrade():
sa.UniqueConstraint('email'),
sa.UniqueConstraint('username')
)
op.create_table('game_rooms',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=40), nullable=False),
sa.Column('description', sa.String(length=200), nullable=False),
sa.Column('private', sa.Boolean(), nullable=False),
sa.Column('language', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['language'], ['room_languages.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('game_rooms_users',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('game_rooms_id', sa.Integer(), nullable=False),
@ -81,16 +64,27 @@ def upgrade():
sa.Column('name', sa.String(length=40), nullable=True),
sa.Column('description', sa.String(length=200), nullable=True),
sa.Column('round', sa.Integer(), nullable=True),
sa.Column('game_room', sa.Integer(), nullable=True),
sa.Column('time_settings', sa.Integer(), nullable=True),
sa.Column('main_time', sa.Enum('BYOYOMI', 'ABSOLUTE', 'HOURGLASS', 'NONE', name='timetypes'), nullable=False),
sa.Column('time_period', sa.Integer(), nullable=True),
sa.Column('period_length', sa.Integer(), nullable=True),
sa.Column('overtime', sa.Enum('BYOYOMI', 'ABSOLUTE', 'HOURGLASS', 'NONE', name='timetypes'), nullable=False),
sa.Column('overtime_period', sa.Integer(), nullable=True),
sa.Column('overtime_length', sa.Integer(), nullable=True),
sa.Column('game_room_id', sa.Integer(), nullable=True),
sa.Column('player_black', sa.Integer(), nullable=True),
sa.Column('player_white', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['game_room'], ['game_rooms.id'], ),
sa.ForeignKeyConstraint(['game_room_id'], ['game_rooms.id'], ),
sa.ForeignKeyConstraint(['player_black'], ['users.id'], ),
sa.ForeignKeyConstraint(['player_white'], ['users.id'], ),
sa.ForeignKeyConstraint(['time_settings'], ['time_settings.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('games_users',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('game_rooms_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['game_rooms_id'], ['games.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('user_id', 'game_rooms_id')
)
op.create_table('moves',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('player', sa.Enum('BLACK', 'WHITE', name='players'), nullable=True),
@ -99,18 +93,18 @@ def upgrade():
sa.Column('move_number', sa.Integer(), nullable=True),
sa.Column('is_pass', sa.Boolean(), nullable=False),
sa.Column('is_main', sa.Boolean(), nullable=False),
sa.Column('game', sa.Integer(), nullable=False),
sa.Column('preceding_move', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['game'], ['games.id'], ),
sa.ForeignKeyConstraint(['preceding_move'], ['moves.id'], ),
sa.Column('game_id', sa.Integer(), nullable=False),
sa.Column('preceding_move_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['game_id'], ['games.id'], ),
sa.ForeignKeyConstraint(['preceding_move_id'], ['moves.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('messages',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('date', sa.DateTime(), nullable=False),
sa.Column('content', sa.String(length=200), nullable=False),
sa.Column('move', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['move'], ['moves.id'], ),
sa.Column('move_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['move_id'], ['moves.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
@ -120,10 +114,9 @@ def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('messages')
op.drop_table('moves')
op.drop_table('games_users')
op.drop_table('games')
op.drop_table('game_rooms_users')
op.drop_table('game_rooms')
op.drop_table('users')
op.drop_table('time_settings')
op.drop_table('room_languages')
op.drop_table('game_rooms')
# ### end Alembic commands ###

View file

@ -3,6 +3,11 @@ from marshmallow import fields
import enum
from models.User import user_schema
# ! Games >-< Users join table
games_users = db.Table('games_users',
db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True),
db.Column('game_rooms_id', db.Integer, db.ForeignKey('games.id'), primary_key=True)
)
class Game(db.Model):
__tablename__ = "games"
@ -20,6 +25,12 @@ class Game(db.Model):
TIME = "The game ended in loss by time out"
VOID = "The game was suspended"
class TimeTypes(enum.Enum):
BYOYOMI = "Counting by time period"
ABSOLUTE = "One period to use time"
HOURGLASS = "Absolute time for both players"
NONE = "Untimed"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
date = db.Column(db.DateTime())
komi = db.Column(db.Numeric(2,1), nullable=False)
@ -36,14 +47,22 @@ class Game(db.Model):
name = db.Column(db.String(40))
description = db.Column(db.String(200))
round = db.Column(db.Integer)
main_time = db.Column(db.Enum(TimeTypes), nullable=False)
time_period = db.Column(db.Integer) # number of periods
period_length = db.Column(db.Integer) # seconds
overtime = db.Column(db.Enum(TimeTypes), nullable=False)
overtime_period = db.Column(db.Integer) # number of overtime periods
overtime_length = db.Column(db.Integer) # seconds
# foreign keys
game_room = db.Column(db.Integer, db.ForeignKey("game_rooms.id"))
time_settings = db.Column(db.Integer, db.ForeignKey("time_settings.id"))
game_room_id = db.Column(db.Integer, db.ForeignKey("game_rooms.id"))
player_black = db.Column(db.Integer, db.ForeignKey("users.id"))
player_white = db.Column(db.Integer, db.ForeignKey("users.id"))
def __init__(self, name, description, board_size, game_room, player_white, komi=0.5, handicap=0, time_settings=1):
def __init__(
self, name, description, board_size, game_room, player_white,
komi=0.5, handicap=0, main_time=TimeTypes.NONE, overtime=TimeTypes.NONE
):
self.name = name
self.description = description
self.board_size = board_size
@ -51,8 +70,8 @@ class Game(db.Model):
self.player_white = player_white
self.komi = komi
self.handicap = handicap
self.time_settings = time_settings
print('did it')
self.main_time = main_time
self.overtime = overtime
class GameSchema(ma.ModelSchema):
id = fields.Int()

View file

@ -1,42 +1,30 @@
from app import db, ma
from marshmallow import fields
import enum
# TODO User >---< GameRoom
# ! Game Room >-< Users join table
# ! Game Rooms >-< Users join table
game_rooms_users = db.Table('game_rooms_users',
db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True),
db.Column('game_rooms_id', db.Integer, db.ForeignKey('game_rooms.id'), primary_key=True)
)
class RoomLanguages(db.Model):
__tablename__ = "room_languages"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(40), nullable=False)
iso = db.Column(db.String(2), nullable=False)
class GameRoom(db.Model):
__tablename__ = "game_rooms"
__table_args__ = {'extend_existing': True}
class Languages(enum.Enum):
EN = "English"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(40), nullable=False)
description = db.Column(db.String(200), nullable=False)
private = db.Column(db.Boolean(), nullable=False, default=False)
# ! Foreign Keys
users = db.relationship(
'User',
secondary=game_rooms_users,
lazy='subquery',
backref=db.backref('game_rooms', lazy=True)
)
language = db.Column(db.Integer, db.ForeignKey("room_languages.id"), nullable=False)
language = db.Column(db.Enum(Languages), nullable=False, default=Languages.EN)
def __init__(self, name, description, private=False, language=1):
def __init__(self, name, description, private=False, language=Languages.EN):
self.name = name
self.description = description
self.private = private

View file

@ -15,7 +15,7 @@ class Message(db.Model):
content = db.Column(db.String(200), nullable=False)
# foreign key
move = db.Column(db.Integer, db.ForeignKey("moves.id"), nullable=False)
move_id = db.Column(db.Integer, db.ForeignKey("moves.id"), nullable=False)
def __init__(self):
pass

View file

@ -19,8 +19,14 @@ class Move(db.Model):
is_main = db.Column(db.Boolean, nullable=False, default=True)
# foreign keys
game = db.Column(db.Integer, db.ForeignKey("games.id"), nullable=False)
preceding_move = db.Column(db.Integer, db.ForeignKey("moves.id"))
game_id = db.Column(db.Integer, db.ForeignKey("games.id"), nullable=False)
preceding_move_id = db.Column(db.Integer, db.ForeignKey("moves.id"))
succeeding_moves = db.relationship(
'Move',
lazy='subquery',
backref=db.backref('moves', lazy=True)
)
def __init__(self):
pass

View file

@ -1,26 +0,0 @@
from app import db, ma
import enum
class TimeSettings(db.Model):
__tablename__ = "time_settings"
__table_args__ = {'extend_existing': True}
class TimeTypes(enum.Enum):
BYOYOMI = "Counting by time period"
ABSOLUTE = "One period to use time"
HOURGLASS = "Absolute time for both players"
NONE = "Untimed"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
main_time = db.Column(db.Enum(TimeTypes), nullable=False)
time_period = db.Column(db.Integer) # number of periods
period_length = db.Column(db.Integer) # seconds
overtime = db.Column(db.Enum(TimeTypes), nullable=False)
overtime_period = db.Column(db.Integer) # number of overtime periods
overtime_length = db.Column(db.Integer) # seconds
def __init__(self, main_time=TimeTypes.NONE, overtime=TimeTypes.NONE):
self.main_time = main_time
self.overtime = overtime

View file

@ -62,6 +62,7 @@ class User(db.Model):
elo = db.Column(db.Integer)
rank_certainty = db.Column(db.Boolean, nullable=False, default=False)
def __init__(self, username, email, password, rank=Ranks.K1, admin=False):
self.username = username
self.email = email