On Wednesday, August 14, 2013 12:10:12 AM UTC-5, Lukasz Szybalski wrote:
>
> Hello,
> How do I go from class like defeinition to below with mapper.
>
>
> The docs in 0.8 say I can use:
>
> from sqlalchemy.ext.declarative import DeferredReflectionBase =
> declarative_base()
> class MyClass(DeferredReflection, Base):
> __tablename__ = 'mytable'
>
>
>
> but how do I do below with DefferedReflection
>
> ------
> from sqlalchemy import Table
> from sqlalchemy.orm import mapper, relation
>
> class Recall(object):
> def __init__(self, **kw):
> """automatically mapping attributes"""
> for key, value in kw.iteritems():
> setattr(self, key, value)
>
>
> recall_table = Table('recall_db', metadata,
> autoload=True,autoload_with=engine)
> mapper(Recall, recall_table,primary_key=[recall_table.c.RECORD_ID])
> ----
>
>
> I'm using pyramid, and I want to autoload tables in models.py which does
> not have the engine bound yet. I will bind in in main function with
>
> DeferredReflection.prepare(engine)
>
>
>
Never mind:
I followed the documentation:
In models.py I did
from sqlalchemy.ext.declarative import DeferredReflection
class Recall(DeferredReflection, Base):
__tablename__ = 'recall_db'
in __init__.py I did
from sqlalchemy.ext.declarative import DeferredReflection
then under
Base.metadata.bind = engine
add
DeferredReflection.prepare(engine)
I can access the table with
Recall.__table__
and mapper with
Recall.__mapper__
I presume this does the same as :
recall_table = Table('recall_db', metadata, autoload=True,autoload_with=
engine)
class Recall(object):
def __init__(self, **kw):
"""automatically mapping attributes"""
for key, value in kw.iteritems():
setattr(self, key, value)
mapper(Recall, recall_table,primary_key=[recall_table.c.RECORD_ID])
Thanks
Lucas
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.