flask sqlalchemy database code example

Example 1: db.relationship sqlalchemy flask

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    addresses = db.relationship('Address', backref='person', lazy=True)

class Address(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120), nullable=False)
    person_id = db.Column(db.Integer, db.ForeignKey('person.id'),
        nullable=False)

Example 2: sqlalchemy flask query

>>> from yourapplication import User
>>> admin = User(username='admin', email='[email protected]')
>>> guest = User(username='guest', email='[email protected]')

Tags:

Sql Example