Ejemplo n.º 1
0
class City(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30), unique=True)

    # optional
    def __repr__(self):
        return f'City <id={self.id}, name={self.name}>'
Ejemplo n.º 2
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
Ejemplo n.º 3
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}>'
Ejemplo n.º 4
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}>'
Ejemplo n.º 5
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}>'
Ejemplo n.º 6
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}>'
Ejemplo n.º 7
0
class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(64), index=True)
    body = db.Column(db.Text)
    author_id = db.Column(db.Integer, db.ForeignKey('author.id'))

    # optional
    def __repr__(self):
        return f'Article <id={self.id}, title={self.title}, body={self.body}, author_id={self.author_id}>'
Ejemplo n.º 8
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}>'
Ejemplo n.º 9
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}]>'
Ejemplo n.º 10
0
class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.Text)
    created_at = db.Column(db.DateTime(timezone=True),
                           server_default=func.now(),
                           onupdate=func.now())

    # optional
    def __repr__(self):
        return f'Note <id={self.id}, body={self.body}, created_at={self.created_at}>'
Ejemplo n.º 11
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}>'
Ejemplo n.º 12
0
class Draft(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.Text)
    edit_time = db.Column(db.Integer, default=0)
Ejemplo n.º 13
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
Ejemplo n.º 14
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}>'


# many to many + bidirectional relationship
student_teacher_table = db.Table(
    'student_teacher',
    db.Column('student_id', db.Integer, db.ForeignKey('student.id')),
    db.Column('teacher_id', db.Integer, db.ForeignKey('teacher.id')))


class Student(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(70), unique=True)
    grade = db.Column(db.String(20))
    teachers = db.relationship('Teacher',
                               secondary=student_teacher_table,
                               back_populates='students')

    def __repr__(self):
        return f'Student <id={self.id}, name={self.name}>'