Flask-SQLAlchemy 中使用显式主主数据库设置

1、问题背景

在一个 Flask-SQLAlchemy 项目中,用户想要使用显式主主数据库设置。具体而言,他想要能够从默认数据库中读取数据,并将数据持久化到两个主数据库中。他希望知道是否可以使用 Flask-SQLAlchemy 和 binds 来实现这一目标。

2、解决方案

为了实现显式主主数据库设置,可以按照以下步骤进行操作:

  1. 定义 SQLAlchemy 应用程序配置
python 复制代码
app = Flask(__name__)

# 定义默认数据库 URI
SQLALCHEMY_DATABASE_URI = 'default_DB_uri'

# 定义主数据库 URI
SQLALCHEMY_BINDS = { 'master1':'first_master_DB_uri', 'master2': 'second_master_DB_uri' }

# 将 SQLAlchemy 配置应用到 Flask 应用中
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_BINDS'] = SQLALCHEMY_BINDS

# 创建 SQLAlchemy 对象
db = SQLAlchemy(app)
  1. 自定义 Flask-SQLAlchemy 会话类
python 复制代码
from flask_sqlalchemy import SQLAlchemy, SignallingSession, get_state
from flask_sqlalchemy._compat import itervalues

class UsingBindSignallingSession(SignallingSession):
    def get_bind(self, mapper=None, clause=None):
        if self._name:
            _eng = get_state(self.app).db.get_engine(self.app,bind=self._name)
            return _eng
        else:
            return super(UsingBindSignallingSession, self).get_bind(mapper, clause)

    _name = None
    def using_bind(self, name):
        self._name = name
        return self

class UsingBindSQLAlchemy(SQLAlchemy):
    def create_session(self, options):
        return UsingBindSignallingSession(self, **options)

    def get_binds(self, app=None):
        retval = super(UsingBindSQLAlchemy, self).get_binds(app)
        # get the binds for None again in order to make sure that it is the default bind for tables
        # without an explicit bind
        bind = None
        engine = self.get_engine(app, bind)
        tables = self.get_tables_for_bind(bind)
        retval.update(dict((table, engine) for table in tables))
        return retval

    def get_tables_for_bind(self, bind=None):
        """Returns a list of all tables relevant for a bind.
        Tables without an explicit __bind_key__ will be bound to all binds.
        """
        result = []
        for table in itervalues(self.Model.metadata.tables):
            # if we don't have an explicit __bind_key__ bind this table to all databases
            if table.info.get('bind_key') == bind or table.info.get('bind_key') == None:
                result.append(table)
        return result

db = UsingBindSQLAlchemy()
  1. 使用自定义的 Flask-SQLAlchemy 会话类
python 复制代码
# 创建一个默认数据库会话
session = db.session

# 创建一个主数据库会话
master_session1 = db.session().using_bind('master1')

# 创建另一个主数据库会话
master_session2 = db.session().using_bind('master2')

# 在默认数据库中读取数据
read_data = session.query('select ...').all()

# 在第一个主数据库中持久化数据
master_session1.add(SOME_OBJECT)
master_session1.commit()

# 在第二个主数据库中持久化数据
master_session2.add(SOME_OBJECT_CLONE)
master_session2.commit()

通过上述步骤,就可以实现显式主主数据库设置,并在 Flask-SQLAlchemy 中使用它。

相关推荐
acaad5 分钟前
Redis下载与安装(Windows)
数据库·redis·缓存
玄〤5 分钟前
黑马点评中 VoucherOrderServiceImpl 实现类中的一人一单实现解析(单机部署)
java·数据库·redis·笔记·后端·mybatis·springboot
SunflowerCoder25 分钟前
EF Core + PostgreSQL 配置表设计踩坑记录:从 23505 到 ChangeTracker 冲突
数据库·postgresql·c#·efcore
短剑重铸之日33 分钟前
《7天学会Redis》Day2 - 深入Redis数据结构与底层实现
数据结构·数据库·redis·后端
七牛云行业应用39 分钟前
重构实录:我删了 5 家大模型 SDK,只留了 OpenAI 标准库
python·系统架构·大模型·aigc·deepseek
知乎的哥廷根数学学派1 小时前
基于多模态特征融合和可解释性深度学习的工业压缩机异常分类与预测性维护智能诊断(Python)
网络·人工智能·pytorch·python·深度学习·机器学习·分类
一人の梅雨1 小时前
亚马逊SP-API商品详情接口轻量化实战:合规与商业价值提取指南
python
Zoey的笔记本1 小时前
「支持ISO27001的GTD协作平台」数据生命周期管理方案与加密通信协议
java·前端·数据库
什么都不会的Tristan2 小时前
MybatisPlus-扩展功能
数据库·mysql
超级种码2 小时前
Redis:Redis 数据类型
数据库·redis·缓存