On Fri, Aug 19, 2011 at 9:23 AM, Mark Erbaugh <[email protected]> wrote:
>
> On Aug 19, 2011, at 10:41 AM, Michael Bayer wrote:
>
> > Id use a mixin so that a superclass can be generated in a data driven
> manner:
> >
> >
> > MyCols = type("MyCols", (object, ), dict(("field%d" % i, Column(Integer))
> for i in xrange(1, 10)))
>
If you are going to do this a lot, it can be a pain with declarative. I
developed a framework that I used, based on ORM, which uses an explicit
_makeTable() and _mapTable() objects that I call in order to create and map
the tables. Since these are python methods, I can use any kind of python
iteration or code I want to decide what columns to create. It is a pretty
flexible model. Example conceptual code:
class Database(object):
table_args = { 'mysql_engine' : 'InnoDB' }
schema = "database_name"
def __init__(self,dbclasses=[]):
self.metadata = MetaData()
self.engine = ... (set up engine, etc.)
self.dbclasses = dbclasses
for c in self.dbclasses:
c._makeTable(self,self.engine)
cls.__table__.create(bind=self.engine,checkfirst=True)
for c in self.dbclasses:
c._mapTable(self)
class FooRecord(object):
@classmethod
def _makeTable(cls,db,engine):
cls.db = db
cls.__table__ = Table('foo', db.metadata,
Column('x'),
Column('y'),
**cls.table_args,
schema=cls.schema
etc.)
@classmethod
def _mapTable(cls,db):
mapper(cls, cls.__table__, properties={ ... })
db = Database([FooRecord])
You may find a model like this easier to use to create dynamically-generated
tables. The point here is that SQLAlchemy is sufficiently flexible so that
if declarative doesn't meet your needs or is a bit cumbersome for what you
want to do, you can just start at the ORM (below declarative) level and
build up a framework that works for you.
Regards,
Daniel
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.