Jan-06-2020, 05:02 PM
(This post was last modified: Jan-06-2020, 05:02 PM by mrdominikku.)
Hi,
Based on below posts
- dynamic-schema
- Dynamic Class Creation in SQLAlchemy
I wanted to make similar thing in my project. I use declarative_base class for core ORM models stored in db_models.py, additionally depends on user settings I want to create and store dynamic classes in BASE._dec_class_registry (db_manage.py>LotteryDatabase>_initial_database>create_class_model) those dynamic classes vary depends on params combinations from games_config.py.
My problem is when the first create_class_model method is called it's correctly adding class to BASE._dec_class_registry, but when calling next one BASE "losing" first class and adding new one and so on. When BASE.metadata.create_all() is called all tables are created in schema, but I can't access this dynamic classes using LotteryDatabase>set_model_by_table_name.
I tried doing it with raw test example:
models.py
Application is fully operational and including requirements.txt file for packages.
For better inside what I am talking about, step by step example using debug mode. (look image below)
I have rewrote code so all class attributes are stored in dictionary and then used in creating models.
-->>
Based on below posts
- dynamic-schema
- Dynamic Class Creation in SQLAlchemy
I wanted to make similar thing in my project. I use declarative_base class for core ORM models stored in db_models.py, additionally depends on user settings I want to create and store dynamic classes in BASE._dec_class_registry (db_manage.py>LotteryDatabase>_initial_database>create_class_model) those dynamic classes vary depends on params combinations from games_config.py.
My problem is when the first create_class_model method is called it's correctly adding class to BASE._dec_class_registry, but when calling next one BASE "losing" first class and adding new one and so on. When BASE.metadata.create_all() is called all tables are created in schema, but I can't access this dynamic classes using LotteryDatabase>set_model_by_table_name.
I tried doing it with raw test example:
models.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Integer, Column
BASE = declarative_base()
class NewClass(BASE):
__tablename__ = 'test'
id = Column('id', Integer, primary_key=True)test.pyfrom models import *
from sqlalchemy import create_engine
DB_ENGINE = 'sqlite:///my_db.db'
def main():
engine = create_engine(DB_ENGINE)
BASE.metadata.create_all(engine)
input_params = {'__tablename__': 'test1',
'id': Column('id', Integer, primary_key=True)}
_ = type('InputClass', (BASE,), input_params)
model_params = {'__tablename__': 'test2',
'id': Column('id', Integer, primary_key=True)}
_ = type('ModelClass', (BASE,), model_params)
predict_params = {'__tablename__': 'test3',
'id': Column('id', Integer, primary_key=True)}
_ = type('PredictClass', (BASE,), predict_params)
BASE.metadata.create_all(engine)All classes where created and added to BASE, so problem is with my code and I am missing something. Your fresh look would be very helpful!Application is fully operational and including requirements.txt file for packages.
For better inside what I am talking about, step by step example using debug mode. (look image below)
I have rewrote code so all class attributes are stored in dictionary and then used in creating models.
-->>
