예제 #1
0
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
예제 #2
0
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}>'
예제 #3
0
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}>'
예제 #4
0
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}>'
예제 #5
0
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}>'
예제 #6
0
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}>'
예제 #7
0
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}]>'
예제 #8
0
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}>'
예제 #9
0
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