简体   繁体   中英

How to choose database binds in flask-sqlalchemy

I have two database binds in Flask - 'default' and 'sus'. I used bind_key in Model, it worked OK.

class Person(db.Model):
    __tablename__ = 'persons'
    __bind_key__ = 'sus'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode)
Person.query.get(5)

But now i need to execute that from 'sus' bind:

from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import func

db = SQLAlchemy(app)
db.session.query(func.do_something(some_params)).first()
db.session.commit()

And i have got the 'default' bind, not 'sus'. What should i do?

Your db.session query is not using the Person model at all, so it doesn't know anything about the __bind_key__

Try to refactor your query so you can use Person.query.filter(...).first()

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