emmett-framework / emmett

The web framework for inventors
Other
1.03k stars 70 forks source link

Use models in multiple databases #485

Closed erhuabushuo closed 5 months ago

erhuabushuo commented 5 months ago

In a database master-slave , I need define same models with two databases. How can I specify which connection when I query by using Model

gi0baro commented 5 months ago

The only existing support you have for this today is defining a subclass for every database instance you want to support, eg:

from emmett.orm import Model, Field

class MyModel(Model):
    tablename = "mymodel"
    foo = Field()

class MyReadModel(MyModel):
    ...

class MyWriteModel(MyModel):
    ...

dbr.define_models(MyReadModel)
dbw.define_models(MyWriteModel)
erhuabushuo commented 5 months ago

Thanks a lot.