Repository: fossasia/open-event-server

Issue:
Fix session_type table
It seems that session_type was implemented keeping in mind that one session_type links to one session.
(i.e. one-to-one relationship) (It is evident from the [comment](https://github.com/fossasia/open-event-orga-server/pull/973/files#diff-1f6df12a90bf96660cf4fa6339090ca2L15))
After some discussion on gitter, it is established that many sessions can link to one session type, and a session will have only one session type. 



Discussion:

Resolving patch:
diff --git a/migrations/versions/1b34cefe5f92_.py b/migrations/versions/1b34cefe5f92_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/1b34cefe5f92_.py
@@ -0,0 +1,33 @@
+"""empty message
+
+Revision ID: 1b34cefe5f92
+Revises: 10136334dff5
+Create Date: 2016-06-18 13:32:26.921934
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '1b34cefe5f92'
+down_revision = '10136334dff5'
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+
+
+def upgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('session', sa.Column('session_type_id', sa.Integer(), nullable=True))
+    op.create_foreign_key(None, 'session', 'session_type', ['session_type_id'], ['id'])
+    op.drop_constraint(u'session_type_session_id_fkey', 'session_type', type_='foreignkey')
+    op.drop_column('session_type', 'session_id')
+    ### end Alembic commands ###
+
+
+def downgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('session_type', sa.Column('session_id', sa.INTEGER(), autoincrement=False, nullable=True))
+    op.create_foreign_key(u'session_type_session_id_fkey', 'session_type', 'session', ['session_id'], ['id'])
+    op.drop_constraint(None, 'session', type_='foreignkey')
+    op.drop_column('session', 'session_type_id')
+    ### end Alembic commands ###
diff --git a/open_event/models/session.py b/open_event/models/session.py
--- a/open_event/models/session.py
+++ b/open_event/models/session.py
@@ -25,6 +25,7 @@ class Session(db.Model):
         backref=db.backref('sessions', lazy='dynamic'))
     language = db.Column(db.String)
     microlocation_id = db.Column(db.Integer, db.ForeignKey('microlocation.id'))
+    session_type_id = db.Column(db.Integer, db.ForeignKey('session_type.id'))
 
     slides = db.Column(db.String)
     video = db.Column(db.String)
@@ -52,7 +53,8 @@ def __init__(self,
                  slides=None,
                  video=None,
                  audio=None,
-                 signup_url=None):
+                 signup_url=None,
+                 session_type=None):
         self.title = title
         self.subtitle = subtitle
         self.short_abstract = short_abstract
@@ -70,12 +72,12 @@ def __init__(self,
         self.video = video
         self.audio = audio
         self.signup_url = signup_url
+        self.session_type = session_type
 
     @staticmethod
     def get_service_name():
         return 'session'
 
-
     @property
     def is_accepted(self):
         return self.state == "accepted"
diff --git a/open_event/models/session_type.py b/open_event/models/session_type.py
--- a/open_event/models/session_type.py
+++ b/open_event/models/session_type.py
@@ -8,7 +8,6 @@ class SessionType(db.Model):
     length = db.Column(db.Float, nullable=False)
     event_id = db.Column(
         db.Integer, db.ForeignKey('events.id', ondelete='CASCADE'))
-    session_id = db.Column(db.Integer, db.ForeignKey('session.id'))
     events = db.relationship("Event", backref="session_type")
     sessions = db.relationship("Session",
                                uselist=False,


Test patch:
diff --git a/tests/test_session_type.py b/tests/test_session_type.py
--- a/tests/test_session_type.py
+++ b/tests/test_session_type.py
@@ -12,17 +12,18 @@
 class TestSessionType(OpenEventTestCase):
     def test_add_session_type_to_db(self):
         """Checks the one to many relationship between event and session_types and
-                the one to one relationship between session and session_types"""
+                the many to one relationship between session and session_types"""
         self.app = Setup.create_app()
         with app.test_request_context():
             event = ObjectMother.get_event()
             session1 = ObjectMother.get_session()
+            session_type1 = SessionType(name='Type1', length='30', event_id='1')
+            session_type2 = SessionType(name='Type2', length='30', event_id='1')
             session2 = Session(title='test2',
                                long_abstract='dsadsd',
                                start_time=datetime(2003, 8, 4, 12, 30, 45),
-                               end_time=datetime(2003, 8, 4, 12, 30, 45))
-            session_type1 = SessionType(name='Type1', length='30', event_id='1', session_id='1')
-            session_type2 = SessionType(name='Type2', length='30', event_id='1', session_id='2')
+                               end_time=datetime(2003, 8, 4, 12, 30, 45),
+                               session_type=session_type1)
             save_to_db(event, "Event Saved")
             save_to_db(session1, "Session1 Saved")
             save_to_db(session2, "Session2 Saved")
@@ -30,8 +31,8 @@ def test_add_session_type_to_db(self):
             save_to_db(session_type2, "SessionType2 Saved")
             self.assertEqual(session_type1.event_id, 1)
             self.assertEqual(session_type2.event_id, 1)
-            self.assertEqual(session_type1.session_id, 1)
-            self.assertEqual(session_type2.session_id, 2)
+            self.assertEqual(session2.session_type_id, 1)
+            self.assertEqual(session1.session_type_id, None)
 
 
 if __name__ == '__main__':