Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 868
stay_hungry Avatar asked Mar 10 '19 02:03

stay_hungry


1 Answers

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()

like image 152
stay_hungry Avatar answered Oct 30 '22 11:10

stay_hungry