Sfoglia il codice sorgente

Update models and created migration

JoostSijm 6 anni fa
parent
commit
2f744b5b5c

+ 2 - 24
app/models.py

@@ -15,22 +15,6 @@ from app import db, argon2, login_manager
 ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
 IMAGE_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
 
-page_file = db.Table(
-    'page_file',
-    db.Column(
-        'page_id',
-        db.Integer,
-        db.ForeignKey('page.id'),
-        primary_key=True
-    ),
-    db.Column(
-        'file_id',
-        db.Integer,
-        db.ForeignKey('file.id'),
-        primary_key=True
-    ),
-)
-
 
 class User(db.Model, UserMixin):
     """Model for User"""
@@ -156,12 +140,6 @@ class File(db.Model):
         backref=db.backref("files", lazy="dynamic")
     )
 
-    files = db.relationship(
-        'Page',
-        secondary=page_file,
-        lazy='subquery',
-        backref=db.backref('pages', lazy=True)
-    )
 
 class Ballot(db.Model):
     """Model for Ballot"""
@@ -186,7 +164,7 @@ class Ballot(db.Model):
     )
     priority = db.relationship(
         "Priority",
-        backref=db.backref("options", lazy="dynamic")
+        backref=db.backref("ballots", lazy="dynamic")
     )
 
 
@@ -205,7 +183,7 @@ class Question(db.Model):
 
     ballot_id = db.Column(
         db.Integer,
-        db.ForeignKey("user.id")
+        db.ForeignKey("ballot.id")
     )
     ballot = db.relationship(
         "Ballot",

+ 101 - 0
migrations/versions/c98e920c06ae_add_ballots_remove_page_file_relation.py

@@ -0,0 +1,101 @@
+"""add_ballots_remove_page_file_relation
+
+Revision ID: c98e920c06ae
+Revises: d692d6e1a31d
+Create Date: 2019-03-29 17:49:34.434786
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'c98e920c06ae'
+down_revision = 'd692d6e1a31d'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    op.create_table('priority',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('name', sa.String(), nullable=False),
+    sa.Column('description', sa.String(), nullable=False),
+    sa.PrimaryKeyConstraint('id', name=op.f('pk_priority'))
+    )
+    op.create_table('ballot',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('name', sa.String(), nullable=False),
+    sa.Column('description', sa.String(), nullable=True),
+    sa.Column('start_at', sa.DateTime(), nullable=True),
+    sa.Column('end_at', sa.DateTime(), nullable=False),
+    sa.Column('user_id', sa.Integer(), nullable=True),
+    sa.Column('priority_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['priority_id'], ['priority.id'], name=op.f('fk_ballot_priority_id_priority')),
+    sa.ForeignKeyConstraint(['user_id'], ['user.id'], name=op.f('fk_ballot_user_id_user')),
+    sa.PrimaryKeyConstraint('id', name=op.f('pk_ballot'))
+    )
+    op.create_table('question',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('name', sa.String(), nullable=False),
+    sa.Column('description', sa.String(), nullable=True),
+    sa.Column('ballot_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['ballot_id'], ['ballot.id'], name=op.f('fk_question_ballot_id_ballot')),
+    sa.PrimaryKeyConstraint('id', name=op.f('pk_question'))
+    )
+    op.create_table('option',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('motivation', sa.String(), nullable=True),
+    sa.Column('user_id', sa.Integer(), nullable=True),
+    sa.Column('question_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['question_id'], ['question.id'], name=op.f('fk_option_question_id_question')),
+    sa.ForeignKeyConstraint(['user_id'], ['user.id'], name=op.f('fk_option_user_id_user')),
+    sa.PrimaryKeyConstraint('id', name=op.f('pk_option'))
+    )
+    op.create_table('vote',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('datetime', sa.DateTime(), nullable=True),
+    sa.Column('option_id', sa.Integer(), nullable=True),
+    sa.Column('user_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['option_id'], ['option.id'], name=op.f('fk_vote_option_id_option')),
+    sa.ForeignKeyConstraint(['user_id'], ['user.id'], name=op.f('fk_vote_user_id_user')),
+    sa.PrimaryKeyConstraint('id', name=op.f('pk_vote'))
+    )
+    op.drop_table('page_file')
+    op.add_column('user', sa.Column('alt', sa.Boolean(), server_default='f', nullable=True))
+    op.add_column('user', sa.Column('discord', sa.String(length=255), nullable=True))
+    op.add_column('user', sa.Column('game_id', sa.BigInteger(), nullable=True))
+    op.add_column('user', sa.Column('party_member', sa.Boolean(), server_default='f', nullable=True))
+    op.add_column('user', sa.Column('question_id', sa.Integer(), nullable=True))
+    op.create_unique_constraint(op.f('uq_user_discord'), 'user', ['discord'])
+    op.create_unique_constraint(op.f('uq_user_email'), 'user', ['email'])
+    op.create_unique_constraint(op.f('uq_user_game_id'), 'user', ['game_id'])
+    op.create_unique_constraint(op.f('uq_user_name'), 'user', ['name'])
+    op.drop_constraint('user_name_key', 'user', type_='unique')
+    op.create_foreign_key(op.f('fk_user_question_id_question'), 'user', 'question', ['question_id'], ['id'])
+
+
+def downgrade():
+    op.drop_constraint(op.f('fk_user_question_id_question'), 'user', type_='foreignkey')
+    op.create_unique_constraint('user_name_key', 'user', ['name'])
+    op.drop_constraint(op.f('uq_user_name'), 'user', type_='unique')
+    op.drop_constraint(op.f('uq_user_game_id'), 'user', type_='unique')
+    op.drop_constraint(op.f('uq_user_email'), 'user', type_='unique')
+    op.drop_constraint(op.f('uq_user_discord'), 'user', type_='unique')
+    op.drop_column('user', 'question_id')
+    op.drop_column('user', 'party_member')
+    op.drop_column('user', 'game_id')
+    op.drop_column('user', 'discord')
+    op.drop_column('user', 'alt')
+    op.create_table('page_file',
+    sa.Column('page_id', sa.INTEGER(), autoincrement=False, nullable=False),
+    sa.Column('file_id', sa.INTEGER(), autoincrement=False, nullable=False),
+    sa.ForeignKeyConstraint(['file_id'], ['file.id'], name='page_file_file_id_fkey'),
+    sa.ForeignKeyConstraint(['page_id'], ['page.id'], name='page_file_page_id_fkey'),
+    sa.PrimaryKeyConstraint('page_id', 'file_id', name='page_file_pkey')
+    )
+    op.drop_table('vote')
+    op.drop_table('option')
+    op.drop_table('question')
+    op.drop_table('ballot')
+    op.drop_table('priority')