class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50)) body = db.Column(db.Text) comments = db.relationship('Comment', back_populates='post', cascade='all, delete-orphan') # collection
class Writer(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) books = db.relationship('Book', back_populates='writer') # optional def __repr__(self): return f'Writer <id={self.id}, name={self.name}>'
class Capital(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) country_id = db.Column(db.Integer, db.ForeignKey('country.id')) country = db.relationship('Country', back_populates='capital') # optional def __repr__(self): return f'Capital <id={self.id}, name={self.name}, country={self.country.name if self.country else None}>'
class Citizen(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) city_id = db.Column(db.Integer, db.ForeignKey('city.id')) city = db.relationship('City') # scalar # optional def __repr__(self): return f'Citizen <id={self.id}, name={self.name}>'
class Book(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), index=True) writer_id = db.Column(db.Integer, db.ForeignKey('writer.id')) writer = db.relationship('Writer', back_populates='books') # optional def __repr__(self): return f'Book <id={self.id}, title={self.title}>'
class Author(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) phone = db.Column(db.String(32), unique=True) articles = db.relationship('Article') # optional def __repr__(self): return f'Author <id={self.id}, name={self.name}, phone={self.phone}>'
class Country(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) capital = db.relationship('Capital', uselist=False, back_populates='country') # optional def __repr__(self): return f'Country <id={self.id}, name={self.name}, capital=[{self.capital}]>'
class Teacher(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(70), unique=True) office = db.Column(db.String(20)) students = db.relationship('Student', secondary=student_teacher_table, back_populates='teachers') def __repr__(self): return f'Teacher <id={self.id}, name={self.name}>'
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') # scalar