peewee - modify db model meta (e.g. schema) dynamically

杀马特。学长 韩版系。学妹 提交于 2020-12-13 03:46:24

问题


In order to insert same data to same set of tables in different schemas (data about the mobile application where app id is primary key but app is cross-platform and exists on different platforms. Schema per platfrom so pp id does not interfere for tables with it as fk).

I already has some infrastructure for it and I wasn't expecting this requirement. For me I see the easiest way is having existing model class binded to some db+schema_1+table_name insert data to first schema then somehow update classs meta to db+schema_2+table_name and call insert again.

For example, if I have the following base class with Meta set:

class BaseClass_1:
    class Meta_1:
        legacy_table_names = False
        database = db
        schema = 'schema_1'

and the another meta for another schema:

class Meta_2:
    legacy_table_names = False
    database = db
    schema = 'schema_2'
    primary_key = CompositeKey('app_id')
    db_table = 'table_name'

and the model class:

class Table(BaseClass_1):
    date = DateTimeField()
    app_id = TextField()

    class Meta:
        primary_key = CompositeKey('app_id')
        db_table = 'table_name'

then if I do the following:

Table.insert_many(some_data)
Table._meta = Meta_2
Table._meta = Meta_2()

I see that there is Meta class or object is set instead of "Metadata" objects as it was before modification. Unfortunately I did not find how to instantiate Metadata object. Is it possible, is this research direction hopeful?

来源:https://stackoverflow.com/questions/58166180/peewee-modify-db-model-meta-e-g-schema-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!