flask-sqlalchemy: Trouble joining tables from two databases (different bind keys). Getting error 1146 (see below)

孤人 提交于 2021-02-08 04:39:25

问题


I'm building a Flask-Restful API using python and sqlalchemy, and I'm trying to join two tables from different databases. It appears that I'm only able to search for tables in one database at a time. Am I missing something?

from flask_sqlalchemy import SQLAlchemy
from flask import Flask, jsonify, request 

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@host:8000/database1'
app.config['SQLALCHEMY_BINDS'] = {
    'database2': 'mysql://username:password@host:8000/database2'
}


db = SQLAlchemy(app)
db.create_all(bind='database2')

class Table1(db.Model):
    __tablename__ = "table1"
    __table_args__ = {'schema':'database1'}
    location_id = db.Column(db.Integer, primary_key=True)

    def __init__(self, location_id):
        self.location_id = location_id
    def __repr__(self):
        return '{}'.format(self.location_id)

class Table2(db.Model):
    __bind_key__ = "database2"
    __tablename__ = "table2"
    __table_args__ = {'schema':'database2'}
    other_id = db.Column(db.Integer, primary_key=True)
    location_id = db.Column(db.Integer, db.ForeignKey('database1.table1.location_id'))

    def __init__(self, other_id, location_id):
        self.other_id = other_id
        self.location_id = location_id

    def __repr__(self):
        return '{}'.format(self.other_id)


@app.route('/', methods=['GET'])
def returnRes():
    session = db.session
    q = session.query(table1).join(table2, table1.location_id==table2.location_id).all()
return str(q)

In my browser, I am getting the error: 'sqlalchemy.exc.ProgrammingError: (_mysql_exceptions.ProgrammingError) (1146, "Table 'database1.table2' doesn't exist").

Both tables indeed exist, as when I change my query to q = session.query(table2).join(table1, table2.location_id==table1.location_id).all() I get an error that database2.table1 doesn't exist.

I'm using python==3.6.1, Flask==0.11.1 and Flask-SQLAlchemy==2.1


回答1:


Adding a data base schema argument to my table classes and adding a foreign key fixed this problem. I found the answer at this link: https://github.com/mitsuhiko/flask-sqlalchemy/issues/172

I've updated the question to reflect the answer in case it helps anyone else.

I'm not sure if the binds are redundant, but I've left them in because they don't seem to interfere with anything.



来源:https://stackoverflow.com/questions/46612971/flask-sqlalchemy-trouble-joining-tables-from-two-databases-different-bind-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!