I wonder if one can also do this with Declarative Base classes using `type` construction? Like, to dynamically create a Declarative Class from some pre-defined namespace dicts:
Base = declarative_base() metas = [{'__tablename__': 'footable'}, ...] columns = [{ 'key': 'field1', 'type': Text}, ...] cls_names = ['Foo', ...] clses = {name: type(name, Base, **dict(metas[i], **{ c['key']: Column(c['type']) for c in columns}) for i, name in enumerate(cls_names)} Now I should have a dict of classes I can instantiate by class name: new_foo = clses['Foo'](...) My particular use case is that I can overload some of the namespaces, to decouple the model namespace (the class attributes) from SQLAlchemy, but my instance will still have the predefined attribute names - I am storing data in either the database or a flatfile and I instantiate either a SQLAlchemy declarative object or a generic `object` depending on a flag; after instancing (whether to run a query or parse the flatfile), my application can refer to the object attributes regardless of where the data comes from. On Monday, March 26, 2018 at 2:42:51 PM UTC-4, Mike Bayer wrote: > > On Mon, Mar 26, 2018 at 1:54 PM, Prashanth Budihal > <prasha...@gmail.com <javascript:>> wrote: > > I have this project(stock market data) where I need many tables(200+) > and > > each one will receive fresh data inserts periodically(every 5 minutes). > I > > have gone thru couple of tutorials about sqlalchemy ORM and they all > show > > how to create a single table and do few inserts. But if I have to create > > 200+ tables then should I create/type manually 200+ mapped classes ? > Thats > > impractical isnt it ? I face same hurdle while doing INSERTS. Can > anybody > > here give me a hint atleast how to go about this ? Which part of > sqlalchemy > > addresses this hurdle. I admit I am a newbie to sql and sqlalchemy so > please > > bear with me if there is already a solution and I havent studied it yet. > > Thanks in advance. > > Python is a fully dynamic language so if you needed 200 classes given > a list of 200 names, you would never have to "type" that manually. > However if this is a really simple case then just use core: > > my_twohundred_names = [ ... names ....] > > with engine.connect() as conn: > for name in my_twohundred_names: > t = Table(name, MetaData(), Column('q', Integer), Column('p', > Integer), ... ) > conn.execute(t.insert(), {"q": 5, "p": 10}) > > something like that. > > if these tables have all kinds of different columns you can reflect them: > > > my_twohundred_names = [ ... names ....] > > with engine.connect() as conn: > for name in my_twohundred_names: > t = Table(name, MetaData(), autoload_with=conn) > conn.execute(t.insert(), {"q": 5, "p": 10}) > > but you need to know what the columns are that you are inserting. > > > > > > -- > > SQLAlchemy - > > The Python SQL Toolkit and Object Relational Mapper > > > > http://www.sqlalchemy.org/ > > > > To post example code, please provide an MCVE: Minimal, Complete, and > > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > > description. > > --- > > You received this message because you are subscribed to the Google > Groups > > "sqlalchemy" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to sqlalchemy+...@googlegroups.com <javascript:>. > > To post to this group, send email to sqlal...@googlegroups.com > <javascript:>. > > Visit this group at https://groups.google.com/group/sqlalchemy. > > For more options, visit https://groups.google.com/d/optout. > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.