flask_sqlalchemy中具有相同表名的多个数据库
我有多个数据库,比如flask_sqlalchemy中具有相同表名的多个数据库,sqlalchemy,flask-sqlalchemy,Sqlalchemy,Flask Sqlalchemy,我有多个数据库,比如order\u db1、order\u db2、order\u db3 但是它们都有表order,但是order\u db1.order中的数据不同于order\u db2.order order\u db1.订单 |id |金额| |1 | 100| order\u db2.order |id |金额| |1 | 200| 如何访问flask_sqlalchemy中具有相同表名的多个数据库 我试着这样做,使用SQLALCHEMY\u-BINDS: # -*- coding:
order\u db1
、order\u db2
、order\u db3
但是它们都有表order
,但是order\u db1.order
中的数据不同于order\u db2.order
order\u db1.订单
|id |金额|
|1 | 100|
order\u db2.order
|id |金额|
|1 | 200|
如何访问flask_sqlalchemy中具有相同表名的多个数据库
我试着这样做,使用SQLALCHEMY\u-BINDS
:
# -*- coding: utf-8 -*-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import logging
class Application(Flask):
def __init__(self, import_name):
super(Application, self).__init__(import_name)
app = Application(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_BINDS'] = {
'order_db1': 'mysql://%s:%s@%s/order_db1' % (mysql_user, quote_plus(mysql_pwd),mysql_host),
'order_db2': 'mysql://%s:%s@%s/order_db2' % (mysql_user, quote_plus(mysql_pwd),mysql_host),
}
它告诉我:
“sqlalchemy.exc.InvalidRequestError:已为此元数据实例定义了表'order'。指定“extend_existing=True”以重新定义现有表对象上的选项和列
但是如果我添加\u table\u args={'extend\u existing':True}
class Order1(db.model):
__bind_key__ = 'order_db1'
__tablename__ = 'order'
__table_args = {'extend_existing': True}
id = db.colum(db.Interger)
amount = db.colum(db.Interger)
class Order2(db.model):
__bind_key__ = 'order_db2'
__tablename__ = 'order'
__table_args = {'extend_existing': True}
id = db.colum(db.Interger)
amount = db.colum(db.Interger)
q1=Order1.query.get(1)
q2=Order2.query.get(1)
q1
中的数据与q2
中的数据相同,表示id=1,金额=200
我想要它像:
q1:id=1,金额=100
问题2:id=1,amount=200绑定和相同表名存在已知问题。看看这个和这个可能 另一种解决方法是使用两个
Sqlalchemy
实例,这对于您的用例可能是可接受的,也可能是不可接受的。请参阅使用SQLite的单文件示例
import random
from flask import Flask, render_template_string
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_BINDS'] = {
'order_db1': 'sqlite:///order_db1.sqlite',
'order_db2': 'sqlite:///order_db2.sqlite',
}
db1 = SQLAlchemy(app)
db2 = SQLAlchemy(app)
class Order1(db1.Model):
__bind_key__ = 'order_db1'
__tablename__ = 'order'
id = db1.Column(db1.Integer, primary_key=True)
amount = db1.Column(db1.Integer)
class Order2(db2.Model):
__bind_key__ = 'order_db2'
__tablename__ = 'order'
id = db2.Column(db2.Integer, primary_key=True)
amount = db2.Column(db2.Integer)
_html_template = '''
<p>Order 1 count:{{order_1_count}}</p>
<p>Order 2 count:{{order_2_count}}</p>
'''
@app.route('/')
def index():
order_1_count = Order1.query.count()
order_2_count = Order2.query.count()
return render_template_string(_html_template, order_1_count=order_1_count, order_2_count=order_2_count)
@app.before_first_request
def build_sample_db():
db1.drop_all()
db2.drop_all()
db1.create_all()
db2.create_all()
# Create 100 records in db1
db1.session.add_all([Order1(amount=random.randint(0, 100)) for _ in enumerate(range(0, 100))])
db1.session.commit()
# Create 1000 records in db1
db2.session.add_all([Order2(amount=random.randint(1000, 10000)) for _ in enumerate(range(0, 1000))])
db2.session.commit()
return app
if __name__ == '__main__':
app.run()
随机导入
从烧瓶导入烧瓶,呈现\u模板\u字符串
从flask_sqlalchemy导入sqlalchemy
app=烧瓶(名称)
app.config['SQLALCHEMY_BINDS']={
“order_db1”:”sqlite:///order_db1.sqlite',
“order_db2”:”sqlite:///order_db2.sqlite',
}
db1=SQLAlchemy(应用程序)
db2=SQLAlchemy(应用程序)
类Order1(db1.Model):
__bind_key_uu='order_db1'
__tablename_uu='order'
id=db1.Column(db1.Integer,主键=True)
amount=db1.Column(db1.Integer)
类Order2(db2.Model):
__bind_key_uu='order_db2'
__tablename_uu='order'
id=db2.Column(db2.Integer,primary_key=True)
amount=db2.Column(db2.Integer)
_html_模板=“”
顺序1计数:{Order_1_count}
顺序2计数:{Order_2_count}
'''
@应用程序路径(“/”)
def index():
order\u 1\u count=Order1.query.count()
order\u 2\u count=Order2.query.count()
返回呈现模板字符串(\u html\u模板,订单数量=订单数量,订单数量=订单数量)
@第一次请求前的应用程序
def build_sample_db():
db1.drop_all()
db2.drop_all()
db1.create_all()
db2.create_all()
#在db1中创建100条记录
在枚举中为u添加所有([Order1(amount=random.randint(01100))(范围(01100)))
db1.session.commit()
#在db1中创建1000条记录
在枚举(范围(0,1000))]中为u添加所有([Order2(amount=random.randint(100010000)))
commit()
返回应用程序
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run()
您是否按照中的说明声明绑定。如果是,请提供代码。是的,我声明了它,并用代码更新了我的问题。存在绑定和相同表名的已知问题。看看这个和这个可能。
import random
from flask import Flask, render_template_string
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_BINDS'] = {
'order_db1': 'sqlite:///order_db1.sqlite',
'order_db2': 'sqlite:///order_db2.sqlite',
}
db1 = SQLAlchemy(app)
db2 = SQLAlchemy(app)
class Order1(db1.Model):
__bind_key__ = 'order_db1'
__tablename__ = 'order'
id = db1.Column(db1.Integer, primary_key=True)
amount = db1.Column(db1.Integer)
class Order2(db2.Model):
__bind_key__ = 'order_db2'
__tablename__ = 'order'
id = db2.Column(db2.Integer, primary_key=True)
amount = db2.Column(db2.Integer)
_html_template = '''
<p>Order 1 count:{{order_1_count}}</p>
<p>Order 2 count:{{order_2_count}}</p>
'''
@app.route('/')
def index():
order_1_count = Order1.query.count()
order_2_count = Order2.query.count()
return render_template_string(_html_template, order_1_count=order_1_count, order_2_count=order_2_count)
@app.before_first_request
def build_sample_db():
db1.drop_all()
db2.drop_all()
db1.create_all()
db2.create_all()
# Create 100 records in db1
db1.session.add_all([Order1(amount=random.randint(0, 100)) for _ in enumerate(range(0, 100))])
db1.session.commit()
# Create 1000 records in db1
db2.session.add_all([Order2(amount=random.randint(1000, 10000)) for _ in enumerate(range(0, 1000))])
db2.session.commit()
return app
if __name__ == '__main__':
app.run()