Menu

#13 Reflection tables on model for tg2.

2.3.0
migrated
nobody
docs
defect
normal
documentation
2.1.5
2012-09-25
2011-03-10
No

Hello,

In tg1 I use mapper in model.py to do a reflect tables from database to SqlAlchemy (SA). With this relflect don't needs create all columns in model again, because talbles already created in database. But in TG2 I try the same, like as working in tg1, but not works for me.

In tg1 I was do for example in model.py:

cadcar_table = Table('cadcar',metadata, autoload=True, schema="car")
class Cadcar(object):
    pass
mapper(Cadcar, cadcar_table)

But this (reflect) not works in tg2. I Would like know how I do this in tg2, like as in tg1.

Thank you!


There are some hints in model.py for how to do this, but basically you need to do the refelection inside init_model so that it can be deferred to later in the application load process when the SQLAlchemy metadata is setup for you.

If you are still having trouble, could you post a traceback and link to it here so we can try to help you better?

Thank!


This seems like something we need to clarify rather than fix.


There's even an example of how to do this in quickstart now.


I'm reopening because I just stumbled upon this error.

I'm using the following as example code.

#1    metadata.reflect(bind=engine)
    from sqlalchemy import Table
    global Site
    class Site(DeclarativeBase):
#1        __table__ = metadata.tables['site']
#2        __table__ = Table('site',metadata,autoload=True)
#3        __table__ = Table('site',metadata,autoload=True,autoload_with=engine)

1- the first table declaration works (and the reflect all at line1) 2- This line does not works (this is the example in quickstart) 3- This line does work

Reflection made with a mysql database.

Discussion

  • Michael J. Pedersen

    • version: 2.1.0 --> 2.1.5
    • milestone: 2.2.0 --> 2.3.0
     
  • Michael J. Pedersen

    • status: open --> migrated