简体   繁体   中英

how to reflect an existing table by using flask_sqlalchemy

I've notice a method:

db.reflect(bind='__all__',app=app)

but i wonder how to use it.
I'd appreciate it if you can help.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'database connect url'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
db.Model.metadata.reflect(bind=db.engine,schema='DATABASE_NAME')

class User(db.Model):
    '''deal with an existing table'''
    __table__ = db.Model.metadata.tables['DATABASE_NAME.TABLE_NAME']

u = User.query.all()
print(u)
db.commit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM