I have two tables: Class_ and Student. I want to build a many to many relationship between them:
class Class_(Base):
__tablename__ = 'classes'
id = Column(Unicode(255), primary_key=True)
class Student(User):
__tablename__ = "student"
id = Column(Unicode(255), primary_key=True)
classes = relationship('Class_', secondary='student_class_map',
backref='students', nullable=False)
class StudentClassMapping(Base):
__tablename__ = 'student_class_map'
student_id = Column(Unicode(255),
ForeignKey('student.id', ondelete='CASCADE'),
primary_key=True)
class_id = Column(Unicode(255),
ForeignKey('classes.id', ondelete='CASCADE'),
primary_key=True)
I want to make sure that each Student object has at least one Class_ object related. So I add an nullable=False when declaring the relationship in Student class. However, it raised an error when running because relationship has no parameter named nullable.
Anyone can tell me the right way to achieve that? Thanks in advance.