from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://user:password@dburl?charset=utf8mb4'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class Author(db.Model):
__tablename__ = 'author'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150))
books = db.relationship('Book', backref = 'author')
class Book(db.Model):
__tablename__ = 'book'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150))
author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
@app.route('/')
def index():
data_list = [obj._asdict() for obj in Author.query.filter_by(age = 18).values(Author.id, Author.name)]
return {'data_list': data_list}