> > Question 2:
> > How to autoload 129 tables in the database?
>
> > #for name in engine.execute("SHOW TABLES"):
> > #   tables[name] = sqlalchemy.Table(name, metadata, autoload=True)
>
> > But how do I get the python class, and mappers for all these tables?
>
> > Thanks,
> > Lucas
>

I think I'm trying to solve the same problem as you :)

I created a dummy module APPNAME.model.reflected and imported it in
APPNAME/model/__init__.py

then

class ReflectedTable(object):
    def __init__(self, **kwargs):
        for k, v in self.iteritems():
            setattr(self, k, v)


def init_model(engine):
    """Call me before using any of the tables or classes in the
model."""

    global metadata

    DBSession.configure(bind=engine)

    def table_names():
        introspect = '''SELECT name FROM sqlite_master
                            WHERE type = 'table';'''
        for row in engine.execute(introspect):
            yield str(row[0])

    def do_reflect(name):
        class_name = name.title()

        reflected_table = Table(name, metadata,
            autoload=True, autoload_with=engine)

        cls = type(class_name, (ReflectedTable,), {})
        mapper(cls, reflected_table)
        setattr(APPNAME.model.reflected, class_name, cls)

    for name in table_names():
        do_reflect(name)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to