How can I generate a class/model based on a preexisting table without creating the class/model by hand. In my case this is only used on flat tables without any relations. Assuming I have the following table in my postgres database:
id | brand_name | cloth_type
1 nike t-shirt
2 adidas t-shirt
3 nike skirt
How can I get an object/instance of the first row (id 1) without to write a model by myself? Is there a way to create an abstract class that is generated on the fly or where the attributes are added based on the table?
I found this, but could not really grasp how to implement it. https://docs.sqlalchemy.org/en/13/orm/extensions/automap.html
What I would like to achieve could look something like this (rough code)
engine = db_connect(db_config_section) # creates engine based on config
file
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
abstract_class = ... # somehow generate abstract class
obj = session.query(abstract_class).get(1)
Thanks for possible replies.
Edit: In the linked solution there is still a class defined with a fixed class name and table name. That is not what I am searching for. The code I want should be indipendent from one table. For example the code that works for the cloth tabel (as in my example) should also work for the follwing table (car):
id | brand_name | car_type | doors
1 bmw suv 4
2 ford pickup 2
3 fiat cuv 2