Python Forum
SQL Alchemy dynamic class - declarative_base losing attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL Alchemy dynamic class - declarative_base losing attributes
#3
your sample code puts the classes in _decl_class_registry fine, below is your code with the classes being retrieved:


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)


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)


input_class = BASE._decl_class_registry["InputClass"]
model_class = BASE._decl_class_registry["ModelClass"]
predict_class = BASE._decl_class_registry["PredictClass"]

from sqlalchemy.orm import Session
print(Session().query(predict_class))
I don't have the resources to go looking at your full application but the basic idea works, if you need help with your app see if you can find folks on the IRC channel to take a look for you, or otherwise try to reverse your code back to the sample above to see what's different.
Reply


Messages In This Thread
RE: SQL Alchemy dynamic class - declarative_base losing attributes - by LeanbridgeTech - Jan-07-2020, 07:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,104 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  SQL Alchemy help to extract sql data into csv files mg24 1 1,973 Sep-30-2022, 04:43 PM
Last Post: Larz60+
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,423 Mar-14-2022, 11:21 PM
Last Post: deanhystad
Question I am struggling with basic json library + sql alchemy w/ mariadb-connector json ->sql BrandonKastning 0 1,609 Mar-04-2022, 09:26 PM
Last Post: BrandonKastning
  Distinguishing different types of class attributes Drone4four 4 2,313 Feb-21-2022, 06:34 PM
Last Post: deanhystad
  Environment seems to keep losing references spacedog 2 1,997 Apr-23-2021, 07:36 PM
Last Post: spacedog
  Calls to Attributes of a Class SKarimi 3 3,571 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  losing memory antioch 1 1,845 Jan-20-2021, 05:29 AM
Last Post: antioch
  Winning/Losing Message Error in Text based Game kdr87 2 3,134 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  how to add class instance attributes from list 999masks 2 2,847 Jul-22-2019, 07:59 AM
Last Post: 999masks

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020