如何使用Peewee查询几个类似的数据库? [英] How to query several similar databases using Peewee?
问题描述
我遇到了有关使用Peewee查询多个数据库的问题:
I'm stuck with a problem about querying multiple databases using Peewee:
- 我有2个现有的mysql数据库(我们分别命名为A和B)(结构类似,因为它是两个Bugzilla数据库)
- 我使用Pwiz生成模型(modelsA.py和modelsB.py)
- 我写这段代码:
.
from modelsA import *
from modelsB import *
问题是:由于modelsB中的类(有时)与modelsA相同,所以models B类会覆盖" models A类,从而无法查询A.
The problem is: as the classes in modelsB are (sometimes) the same than modelsA, modelsB classes "overwrite" modelsA classes, making impossible to query A.
此外,我不了解如何查询特定数据库之一. 例如,使用以下代码:
Besides, I don't understand how to query one of the specific database. For example, with this code:
c = Customers.get(Customers.customernumber == 12)
您如何知道要在哪个数据库上执行此查询?
How do you know on which database this query is gonna be executed?
我有一个主意,但对我来说似乎很肮脏: 我可以手动重命名来自modelsA.py和modelsB.py的类,以使其与众不同,然后编写以下代码:
I had an idea, but it seems dirty to me: I could manually rename classes from modelsA.py and modelsB.py to make them distincts and then write this code:
ca = CustomersA.get(CustomersA.customernumber == 12)
cb = CustomersB.get(CustomersB.customernumber == 12)
大致上,Peewee能够处理这类案件吗?如果是这样,该怎么办?片段将不胜感激^^ 谢谢.
Roughly, is Peewee able to deal with these kind of cases? If so, what is the way to do? Snippets would be very appreciated ^^ Thanks.
推荐答案
下一步可能无法完全解决您的问题,但是我自己尝试成功-正在使用 playhouse.Proxy 实例对于我要使用的每个架构,并在innerclass Meta中引用相应的代理.我想这也可以不用代理.但是,您似乎正在寻找跨模式查询,并且已经了解了我刚才提出的内容.
Next is maybe not exact an answer to your problem, but what I tried myself - succesfully - is using a playhouse.Proxy instance for every schema I want to use, and refer to a corresponding proxy in the innerclass Meta. I guess this will work without proxies too. However, seems you are looking for cross-schema queries, and already figured out what I came up with just now.
#!/usr/bin/python
import sqlite3
import peewee
from peewee import *
from playhouse.proxy import *
database_a_proxy = Proxy()
database_b_proxy = Proxy()
class BaseModelA(Model):
class Meta:
database = database_a_proxy
class BaseModelB(Model):
class Meta:
database = database_b_proxy
class RelationInSchemaA(BaseModelA):
textfield = CharField()
class RelationInSchemaB(BaseModelB):
textfield = CharField()
database_a = SqliteDatabase('schemaA', **{})
database_b = SqliteDatabase('schemaB', **{})
database_a_proxy.initialize(database_a)
database_b_proxy.initialize(database_b)
try:
RelationInSchemaA.create_table()
RelationInSchemaB.create_table()
except:
pass
RelationInSchemaA.create(textfield='Hello')
RelationInSchemaB.create(textfield='PeeWee')
好吧,这可以通过手工制作从pwiz.py生成的代码来实现.我敢肯定,也可以使用某种工厂采用一种更优雅,更懒惰 的方式(即不急于)来做到这一点,但是我没有花很多时间在Python和PeeWee上.如果是这样的话,我想pwiz.py也应该为此目的添加一个额外的标志.
Well, this is possible with handcrafting generated code from pwiz.py. I am sure there is a more elegant and lazy (i.e. not eager) way to do this, too, using some kind of factory, but I did not spend much time on Python nor PeeWee yet. If so, pwiz.py should have an extra flag for this purpose too, I guess.
这篇关于如何使用Peewee查询几个类似的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!