2013-05-15 6 views
5

私はクロスデータベースをやろうとしているフラスコSQLAlchemyの中で参加する:これによりクロスデータベースはフラスコSQLAlchemyのに参加

app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = '...Master...' 
app.config['SQLALCHEMY_BINDS'] = { 'Billing': '...Billing...' } 
db = SQLAlchemy(app) 

class Account(db.Model): 
    __tablename__ = 'Accounts' 
    id = db.Column(db.Integer, primary_key=True) 
    username = db.Column(db.String(255)) 

class Setting(db.Model): 
    __tablename__ = 'Settings' 
    AccountId = db.Column(db.Integer, db.ForeignKey(Account.id), primary_key=True) 
    Enabled = db.Column(db.Boolean) 

class BillingAccount(db.Model): 
    __tablename__ = 'Account' 
    __bind_key__ = 'Billing' 
    id = db.Column(db.Integer, primary_key=True) 
    AccountId = db.Column(db.Integer, db.ForeignKey(Account.id)) 
    currency = db.Column(db.Integer) 

class AccountSetting(db.Model): 
    __table__ = db.join(Account, AccountSetting) 
    id = db.column_property(Account.id, AccountSetting.AccountId) 
    username = Account.username 
    enabled = Setting.Enabled 

class AccountSettingBilling(db.Model): 
    __table__ = db.join(Account, AccountSetting).join(BillingAccount) 

    id = db.column_property(Account.id, AccountSetting.AccountId, BillingAccount.AccountId) 
    username = Account.username 
    enabled = Setting.Enabled 
    currency = BillingAccount.currency 

私は成功しAccountSetting.query.allを(照会することができる)が、AccountSettingBillingありません.query.all()は、エラー208(「オブジェクトが存在しません」のMSSQL)で失敗します。

生成されたSQLを調べると、Accounting.AccountId = Accounts.idでJOINを実行していることがわかります。 Billing.Account.AccountId = Accounts.id。

Cross database join in sqlalchemyhttp://pythonhosted.org/Flask-SQLAlchemy/binds.htmlの後には、正しく処理したように見えます。何がありますか?

答えて

1

オブジェクトdb = SQLAlchemy(app)を定義します。これはDatabase1です。どこでも参照できますが、Database2への参照はありません。

Billing . Account . AccountId and [Accounts] . Accounts . id 

あなたは、各クラスの定義からデータベース名のために、このプロパティが欠落している:あなたは3パート識別子を持つことを望む一方で

Account . AccountId and Accounts . id 

:また、2つのパート識別子を使用して参加するためのコードが列を指します:

__table_args__ = {'schema': 'Accounts'} 
__table_args__ = {'schema': 'Billing'} 
関連する問題