From 3022c377b419ad289920f5a370960cdd4e564272 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Mon, 16 May 2022 17:16:42 +0200 Subject: [PATCH] Use back_populates instead of backref --- advlabdb/models.py | 68 +++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/advlabdb/models.py b/advlabdb/models.py index b9f9095..a8157ae 100644 --- a/advlabdb/models.py +++ b/advlabdb/models.py @@ -44,7 +44,7 @@ class Student(db.Model): bachelor_thesis_work_group = db.Column(db.String, nullable=True) note = db.Column(db.Text, nullable=True) - part_students = db.relationship("PartStudent", backref="student", lazy=True) + part_students = db.relationship("PartStudent", back_populates="student", lazy=True) def repr(self): return f"{self.first_name} {self.last_name}" @@ -63,10 +63,13 @@ class PartStudent(db.Model): ) student_id = db.Column(db.Integer, db.ForeignKey("student.id"), nullable=False) + student = db.relationship("Student", back_populates="part_students") part_id = db.Column(db.Integer, db.ForeignKey("part.id"), nullable=False) + part = db.relationship("Part", back_populates="part_students") group_id = db.Column(db.Integer, db.ForeignKey("group.id"), nullable=True) + group = db.relationship("Group", back_populates="part_students") - experiment_marks = db.relationship("ExperimentMark", backref="part_student", lazy=True) + experiment_marks = db.relationship("ExperimentMark", back_populates="part_student", lazy=True) __table_args__ = ( db.UniqueConstraint(student_id, part_id), @@ -142,10 +145,12 @@ class Group(db.Model): number = db.Column(db.Integer, db.CheckConstraint(f"number >= {MIN_GROUP_NUMBER}"), nullable=False) semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=False) + semester = db.relationship("Semester", back_populates="groups") program_id = db.Column(db.Integer, db.ForeignKey("program.id"), nullable=False) + program = db.relationship("Program", back_populates="groups") - part_students = db.relationship("PartStudent", backref="group", lazy=True) - group_experiments = db.relationship("GroupExperiment", backref="group", lazy=True) + part_students = db.relationship("PartStudent", back_populates="group", lazy=True) + group_experiments = db.relationship("GroupExperiment", back_populates="group", lazy=True) __table_args__ = (db.UniqueConstraint(number, semester_id, program_id),) @@ -195,10 +200,12 @@ class GroupExperiment(db.Model): id = db.Column(db.Integer, primary_key=True) semester_experiment_id = db.Column(db.Integer, db.ForeignKey("semester_experiment.id"), nullable=False) + semester_experiment = db.relationship("SemesterExperiment", back_populates="group_experiments") group_id = db.Column(db.Integer, db.ForeignKey("group.id"), nullable=False) + group = db.relationship("Group", back_populates="group_experiments") - appointments = db.relationship("Appointment", backref="group_experiment", lazy=True) - experiment_marks = db.relationship("ExperimentMark", backref="group_experiment", lazy=True) + appointments = db.relationship("Appointment", back_populates="group_experiment", lazy=True) + experiment_marks = db.relationship("ExperimentMark", back_populates="group_experiment", lazy=True) __table_args__ = (db.UniqueConstraint(semester_experiment_id, group_id),) @@ -242,8 +249,9 @@ class Experiment(db.Model): active = db.Column(db.Boolean, default=True, nullable=False) program_id = db.Column(db.Integer, db.ForeignKey("program.id"), nullable=False) + program = db.relationship("Program", back_populates="experiments") - semester_experiments = db.relationship("SemesterExperiment", backref="experiment", lazy=True) + semester_experiments = db.relationship("SemesterExperiment", back_populates="experiment", lazy=True) __table_args__ = (db.UniqueConstraint(number, program_id),) @@ -285,12 +293,14 @@ class SemesterExperiment(db.Model): ) experiment_id = db.Column(db.Integer, db.ForeignKey("experiment.id"), nullable=False) + experiment = db.relationship("Experiment", back_populates="semester_experiments") semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=False) + semester = db.relationship("Semester", back_populates="semester_experiments") assistants = db.relationship( - "Assistant", secondary=experiment_assistant, lazy=True, backref=db.backref("semester_experiments", lazy=True) + "Assistant", secondary=experiment_assistant, back_populates="semester_experiments", lazy=True ) - group_experiments = db.relationship("GroupExperiment", backref="semester_experiment", lazy=True) + group_experiments = db.relationship("GroupExperiment", back_populates="semester_experiment", lazy=True) __table_args__ = (db.UniqueConstraint(experiment_id, semester_id),) @@ -328,9 +338,13 @@ class Assistant(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False, unique=True) + user = db.relationship("User", back_populates="assistant") - appointments = db.relationship("Appointment", backref="assistant", lazy=True) - experiment_marks = db.relationship("ExperimentMark", backref="assistant", lazy=True) + semester_experiments = db.relationship( + "SemesterExperiment", secondary=experiment_assistant, back_populates="assistants", lazy=True + ) + appointments = db.relationship("Appointment", back_populates="assistant", lazy=True) + experiment_marks = db.relationship("ExperimentMark", back_populates="assistant", lazy=True) def repr(self): return f"{self.user.first_name} {self.user.last_name}" @@ -343,8 +357,9 @@ class Admin(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False, unique=True) + user = db.relationship("User", back_populates="admin") - experiment_marks = db.relationship("ExperimentMark", backref="admin", lazy=True) + experiment_marks = db.relationship("ExperimentMark", back_populates="admin", lazy=True) def repr(self): return f"{self.user.first_name} {self.user.last_name}" @@ -359,7 +374,9 @@ class Appointment(db.Model): special = db.Column(db.Boolean, default=False, nullable=False) # In the break or not group_experiment_id = db.Column(db.Integer, db.ForeignKey("group_experiment.id"), nullable=False) + group_experiment = db.relationship("GroupExperiment", back_populates="appointments") assistant_id = db.Column(db.Integer, db.ForeignKey("assistant.id"), nullable=False) + assistant = db.relationship("Assistant", back_populates="appointments") def checkAndGetAssistant(groupExperiment, assistant=None): semesterExperiment = groupExperiment.semester_experiment @@ -403,9 +420,11 @@ class Part(db.Model): number = db.Column(db.Integer, db.CheckConstraint(f"number >= {MIN_PART_NUMBER}"), nullable=False) program_id = db.Column(db.Integer, db.ForeignKey("program.id"), nullable=False) + program = db.relationship("Program", back_populates="parts") semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=False) + semester = db.relationship("Semester", back_populates="parts") - part_students = db.relationship("PartStudent", backref="part", lazy=True) + part_students = db.relationship("PartStudent", back_populates="part", lazy=True) __table_args__ = (db.UniqueConstraint(program_id, number, semester_id),) @@ -421,10 +440,10 @@ class Semester(db.Model): label = db.Column(db.String(10), db.CheckConstraint("label IN ('WS', 'SS')"), nullable=False) year = db.Column(db.Integer, db.CheckConstraint(f"year BETWEEN {MIN_YEAR} AND {MAX_YEAR}"), nullable=False) - parts = db.relationship("Part", backref="semester", lazy=True) - semester_experiments = db.relationship("SemesterExperiment", backref="semester", lazy=True) - active_users = db.relationship("User", backref="active_semester", lazy=True) - groups = db.relationship("Group", backref="semester", lazy=True) + parts = db.relationship("Part", back_populates="semester", lazy=True) + semester_experiments = db.relationship("SemesterExperiment", back_populates="semester", lazy=True) + active_users = db.relationship("User", back_populates="active_semester", lazy=True) + groups = db.relationship("Group", back_populates="semester", lazy=True) __table_args__ = (db.UniqueConstraint(label, year),) @@ -504,11 +523,15 @@ class ExperimentMark(db.Model): ) part_student_id = db.Column(db.Integer, db.ForeignKey("part_student.id"), nullable=False) + part_student = db.relationship("PartStudent", back_populates="experiment_marks") group_experiment_id = db.Column(db.Integer, db.ForeignKey("group_experiment.id"), nullable=False) + group_experiment = db.relationship("GroupExperiment", back_populates="experiment_marks") assistant_id = db.Column( db.Integer, db.ForeignKey("assistant.id"), nullable=True ) # The last assistant who edited the mark + assistant = db.relationship("Assistant", back_populates="experiment_marks") admin_id = db.Column(db.Integer, db.ForeignKey("admin.id"), nullable=True) # The last admin who edited the mark + admin = db.relationship("Admin", back_populates="experiment_marks") __table_args__ = (db.UniqueConstraint(part_student_id, group_experiment_id),) @@ -548,9 +571,10 @@ class User(db.Model, FsUserMixin): room = db.Column(db.String(100), nullable=True) active_semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=True) + active_semester = db.relationship("Semester", back_populates="active_users") - admin = db.relationship("Admin", backref="user", lazy=False, uselist=False) - assistant = db.relationship("Assistant", backref="user", lazy=True, uselist=False) + admin = db.relationship("Admin", back_populates="user", lazy=False, uselist=False) + assistant = db.relationship("Assistant", back_populates="user", lazy=True, uselist=False) def repr(self): return f"{self.first_name} {self.last_name}" @@ -571,9 +595,9 @@ class Program(db.Model): id = db.Column(db.Integer, primary_key=True) label = db.Column(db.String(25), nullable=False, unique=True) - parts = db.relationship("Part", backref="program", lazy=True) - experiments = db.relationship("Experiment", backref="program", lazy=True) - groups = db.relationship("Group", backref="program", lazy=True) + parts = db.relationship("Part", back_populates="program", lazy=True) + experiments = db.relationship("Experiment", back_populates="program", lazy=True) + groups = db.relationship("Group", back_populates="program", lazy=True) def repr(self): return f"{self.label}"