在相同的Flask-SQLAlchemy模型中使用多个POSTGRES数据库和架构 [英] Using multiple POSTGRES databases and schemas with the same Flask-SQLAlchemy model

查看:432
本文介绍了在相同的Flask-SQLAlchemy模型中使用多个POSTGRES数据库和架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在这里非常具体,因为已经提出了类似的问题,但是没有一个解决方案可以解决这个问题。

I'm going to be very specific here, because similar questions have been asked, but none of the solutions work for this problem.

我正在研究一个有四个postgres数据库的项目,但为简单起见,我们说有2个。即A& B

I'm working on a project that has four postgres databases, but let's say for the sake of simplicity there are 2. Namely, A & B

A,B代表两个地理位置,但是数据库中的表和模式是相同的。

A,B represent two geographical locations, but the tables and schemas in the database are identical.

示例模型:

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base

db = SQLAlchemy()
Base = declarative_base()

class FRARecord(Base):
    __tablename__ = 'tb_fra_credentials'

    recnr = Column(db.Integer, primary_key = True)
    fra_code = Column(db.Integer)
    fra_first_name = Column(db.String)

此模型已在两个数据库中复制,但是具有不同的模式,因此要使其在A中工作,我需要这样做:

This model is replicated in both databases, but with different schemas, so to make it work in A, I need to do:

__table_args__ = {'schema' : 'A_schema'}

我想使用一个可以访问数据库但具有相同方法的单一内容提供程序:

I'd like to use a single content provider that is given the database to access, but has identical methods:

class ContentProvider():
    def __init__(self, database):
        self.database = database

    def get_fra_list():
        logging.debug("Fetching fra list")
        fra_list = db.session.query(FRARecord.fra_code)

两个问题是,我该怎么办确定要指向的数据库,以及如何不为不同的模式复制模型代码(这是Postgres特定的问题)

Two problems are, how do I decide what db to point to and how do I not replicate the model code for different schemas (this is a postgres specific problem)

这是到目前为止我尝试过的方法:

Here's what I've tried so far:

1)我为每个模型制作了单独的文件并继承了它们,因此:

1) I've made separate files for each of the models and inherited them, so:

class FRARecordA(FRARecord):
    __table_args__ = {'schema' : 'A_schema'}

这似乎不起作用,因为出现错误:

This doesn't seem to work, because I get the error:

"Can't place __table_args__ on an inherited class with no table."

意思是我无法在db.Model(在其父级中)之后设置该参数声明的

Meaning that I can't set that argument after the db.Model (in its parent) was already declared

2)所以我尝试对多重继承进行相同的操作,

2) So I tried to do the same with multiple inheritance,

class FRARecord():
    recnr = Column(db.Integer, primary_key = True)
    fra_code = Column(db.Integer)
    fra_first_name = Column(db.String)

class FRARecordA(Base, FRARecord):
    __tablename__ = 'tb_fra_credentials'
    __table_args__ = {'schema' : 'A_schema'}

,但得到了可预测的错误:

but got the predictable error:

"CompileError: Cannot compile Column object until its 'name' is assigned."

很明显,我不能将Column对象移动到FRARecordA模型,而不必为B重复它们好(实际上有4个数据库和更多模型)。

Obviously I can't move the Column objects to the FRARecordA model without having to repeat them for B as well (and there are actually 4 databases and a lot more models).

3)最后,我正在考虑进行某种分片(这似乎是正确的)方法),但我找不到如何进行此操作的示例。我的感觉是,我只会使用这样的单个对象:

3) Finally, I'm considering doing some sort of sharding (which seems to be the correct approach), but I can't find an example of how I'd go about this. My feeling is that I'd just use a single object like this:

class FRARecord(Base):
    __tablename__ = 'tb_fra_credentials'

    @declared_attr
    def __table_args__(cls):
        #something where I go through the values in bind keys like
        for key, value in self.db.app.config['SQLALCHEMY_BINDS'].iteritems():
            # Return based on current session maybe? And then have different sessions in the content provider?

    recnr = Column(db.Integer, primary_key = True)
    fra_code = Column(db.Integer)
    fra_first_name = Column(db.String)

要清楚一点,我访问不同数据库的意图如下:

Just to be clear, my intention for accessing the different databases was as follows:

app.config['SQLALCHEMY_DATABASE_URI']='postgresql://%(user)s:\
%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES_A

app.config['SQLALCHEMY_BINDS']={'B':'postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES_B,
                                  'C':'postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES_C,
                                  'D':'postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES_D
                                 }

假设POSTGRES词典包含所有用于连接数据的键

Where the POSTGRES dictionaries contained all the keys to connect to the data

我假设使用继承的对象,那么我将连接到正确的键,例如thi s(因此sqlalchemy查询将自动知道):

I assumed with the inherited objects, I'd just connect to the correct one like this (so the sqlalchemy query would automatically know):

class FRARecordB(FRARecord):
    __bind_key__ = 'B'
    __table_args__ = {'schema' : 'B_schema'}


推荐答案

最终找到了解决方案。

基本上,我没有为每个数据库创建新类,而是为每个数据库使用了不同的数据库连接。

Essentially, I didn't create new classes for each database, I just used different database connections for each.

这种方法本身很常见,棘手的部分(我找不到示例)处理模式差异。我最终这样做:

This method on its own is pretty common, the tricky part (which I couldn't find examples of) was handling schema differences. I ended up doing this:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

Session = sessionmaker()

class ContentProvider():

    db = None
    connection = None
    session = None

    def __init__(self, center):
        if center == A:
            self.db = create_engine('postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES_A, echo=echo, pool_threadlocal=True)
            self.connection = self.db.connect()
            # It's not very clean, but this was the extra step. You could also set specific connection params if you have multiple schemas
            self.connection.execute('set search_path=A_schema')
        elif center == B:
            self.db = create_engine('postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES_B, echo=echo, pool_threadlocal=True)
            self.connection = self.db.connect()
            self.connection.execute('set search_path=B_schema')

    def get_fra_list(self):
        logging.debug("Fetching fra list")
        fra_list = self.session.query(FRARecord.fra_code)
        return fra_list

这篇关于在相同的Flask-SQLAlchemy模型中使用多个POSTGRES数据库和架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆