Hi All,
I'm using flask-sqlalchemy. I have a many-to-many relationship between a professor table and a course table as follow:
course_professors = db.Table('course_professors',
db.Column('course_id', db.Integer, db.ForeignKey('course.id')),
db.Column('professor_id', db.Integer, db.ForeignKey('professor.id'))
)
class Professor(db.Model):
id = db.Column(db.Integer, primary_key = True )
professor_name = db.Column (db.String(80), unique= True, nullable = False, index = True)
courses = db.relationship('Course', secondary = course_professors , backref = db.backref('professor', collection_class=set),\
collection_class=set)
class Course(db.Model):
id = db.Column(db.Integer, primary_key = True )
course_name =db.Column(db.String(120), index = True, nullable = False)
course_description = db.Column(db.Text, nullable = False)
What I want is that the combination (course_name, professor_name) be unique in the database, in order words I want to prevent (course_name, professor_name) duplicates. Note that it is perfectly fine if the same course name appears more than once in the table, but I just don't want the combination (course_name, professor_name) to appear more than once. I have vainly tried a couple of ways to fix this. For example I have used collection_class=set on both ends of the relationship. Please note that applying a Unique key constraint as:
(UniquekeyConstraint(course_id, professor_id))
in the association table won't work either because two courses that have the same name don't have the same id.So what I want is a way to apply the unique constraint on the course name and professor name. Can somebody please help me with that ? Thank you.