0

I am creating an API rest in flask for testing. I'd like to add some data before anything. That's why I've created two csv, one for "Group" (size 3) and another for "Student" (size 90). You can see them here:

enter image description here

enter image description here

They share a relation Many-To-Many what makes me doubt about how to do it. I found an answer here but it's a single table. To keep things simple, I was going to divide students in 3 groups, 30 each (yes, it is a many-to-many but it does not matter if a student belongs to one group only). I don't know if I should create a third dataset with id's but the main problem is how to fill the database with those csv that share a relation Many-to-Many.

You can see here my code so far:

from . import db
from sqlalchemy import Table, ForeignKey
 
association_table = Table('groupstudent', db.metadata,
   db.Column('group_id', ForeignKey('group.id'), primary_key = True),
   db.Column('student_id', ForeignKey('student.id'), primary_key = True))
 
class Group(db.Model):
   __tablename__ = 'group'
   id = db.Column(db.Integer, primary_key=True)
   subject = db.Column(db.String(50))
   name = db.Column(db.String(50))
   description = db.Column(db.String(200))
   language = db.Column(db.String(6))
   age_min = db.Column(db.Integer)
   age_max = db.Column(db.Integer)
   students = relationship("Student", secondary=association_table)
 
class Student(db.Model):
   __tablename__ = 'student'
   id = db.Column(db.Integer, primary_key=True)
   name = db.Column(db.String(50))
   surname = db.Column(db.String(50))
   CCL = db.Column(db.Float())
   CMCT = db.Column(db.Float())
   CD = db.Column(db.Float())
   CPAA = db.Column(db.Float())
   CSC = db.Column(db.Float())
   SIE = db.Column(db.Float())
   CEC = db.Column(db.Float())

and in "init":

def create_database(app):
   if not path.exists('/website'+DB_NAME):
       db.create_all(app=app)
       print('Created!')

Thanks

UPDATE

I found a solution by myself. Here the code, it is not the best, but for testing it works:

        db.create_all(app=app)
        #Students
        file_name = 'students.csv'
        df = pd.read_csv(file_name)
        engine = db.get_engine(app=app)
        df.to_sql(con=engine, index_label='id', name='student', if_exists='replace')
        #Groups
        file_name = 'groups.csv'
        df = pd.read_csv(file_name)
        df.to_sql(con=engine, index_label='id', name='group', if_exists='replace')
        #Many-to-Many relation
        session = Session(engine)
        group0 = session.query(Group).filter(Group.id == 0).first()
        group1 = session.query(Group).filter(Group.id == 1).first()
        group2 = session.query(Group).filter(Group.id == 2).first()
        rows0 = session.query(Student).filter(Student.id >= 0,Student.id <=29).all()
        rows1 = session.query(Student).filter(Student.id >= 30,Student.id <=59).all()
        rows2 = session.query(Student).filter(Student.id >= 60,Student.id <=89).all()
        
        group0.students = [r for r in rows0]
        group1.students = [r for r in rows1]
        group2.students = [r for r in rows2]
        session.commit()
        print('Created Database!')
Laura Galera
  • 89
  • 1
  • 10
  • A many-to-many relationship is really just a combination of two one-to-many relationships. The association table (groupstudent) is a child table to both parent tables (group, and student). Therefore you need to populate the two parent tables first, and then populate the association table with the PK values for each of the two parents. Does that help clarify things? – Gord Thompson Jul 21 '21 at 16:50
  • Well that is something I already had in mind. What I was looking for was an answer that explained how to do that in practice. – Laura Galera Jul 22 '21 at 07:04

0 Answers0