Esempio 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}>'
Esempio 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
Esempio 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}>'
Esempio 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}>'
Esempio 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}>'
Esempio 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}>'
Esempio 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}>'
Esempio n. 8
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}]>'