[英]How to query several similar databases using Peewee?
I'm stuck with a problem about querying multiple databases using Peewee: 我遇到了有关使用Peewee查询多个数据库的问题:
. 。
from modelsA import *
from modelsB import *
The problem is: as the classes in modelsB are (sometimes) the same than modelsA, modelsB classes "overwrite" modelsA classes, making impossible to query A. 问题是:由于modelsB中的类(有时)与modelsA相同,因此modelsB类会“覆盖” modelsA类,从而无法查询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? 您如何知道要在哪个数据库上执行此查询?
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: 我有一个主意,但对我来说似乎很肮脏:我可以手动重命名来自modelsA.py和modelsB.py的类以使其与众不同,然后编写以下代码:
ca = CustomersA.get(CustomersA.customernumber == 12)
cb = CustomersB.get(CustomersB.customernumber == 12)
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. 片段将不胜感激^^谢谢。
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. 接下来可能并不是您问题的确切答案,但是我成功地尝试了一下—为我要使用的每个模式都使用了playhouse.Proxy实例,并引用了内部类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')
Well, this is possible with handcrafting generated code from pwiz.py. 好吧,这可以通过手工制作从pwiz.py生成的代码来实现。 I am sure there is a more elegant and lazy (ie not eager ) way to do this, too, using some kind of factory, but I did not spend much time on Python nor PeeWee yet. 我敢肯定,使用某种工厂也有一种更优雅,更懒惰 (即不急于 )的方式来执行此操作,但是我还没有在Python或PeeWee上花费很多时间。 If so, pwiz.py should have an extra flag for this purpose too, I guess. 如果是这样,我想pwiz.py也应该为此目的额外设置一个标志。
I think this way is simpler: 我认为这种方式更简单:
import modelsA as A
import modelsB as B
ca = A.Customers.get(A.Customers.customernumber == 12)
cb = B.Customers.get(B.Customers.customernumber == 12)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.