I tried your example in sqlalchemy 0.6 by manually plugging in api.py library (attached) that I got from here<https://bitbucket.org/miracle2k/sqlalchemy/src/2d28ed97d3221a133b4b297a229deb294088affe/lib/sqlalchemy/ext/declarative/api.py?at=default> .
I get this error: File "path\to\sample_orm.py", line 33, in map_ props[v] = cls.__table__.c[k] File "path\to\lib\python2.6\sqlalchemy\util.py", line 731, in __getitem__ KeyError: 'Description' Here's my code: from sqlalchemy.orm import mapper from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from .api import DeferredReflection, declared_attr --> this is coming from attached api.py file Base = declarative_base() e = create_engine("sqlite://", echo=True) e.execute(""" create table Sample ( Id integer primary key, Name varchar, Description varchar, IsActive varchar ) """) class MagicMappyThing(DeferredReflection): @declared_attr def __mapper_cls__(cls): def map_(cls, *arg, **kw): props = kw.setdefault("properties", {}) for k, v in cls.__map__.items(): props[v] = cls.__table__.c[k] return mapper(cls, *arg, **kw) return map_ class Sample(MagicMappyThing, Base): __tablename__ = 'Sample' __map__ = {'Id': 'id', 'Name': 'name', 'Description': 'description', 'IsActive': 'is_active'} def __init__(self, id, name, description, is_active): self.id = id self.name = name self.description = description self.is_active = is_active MagicMappyThing.prepare(e) # s = Session(e) # s.add(Sample(id=1, name='some name', description='foo', is_active='foo')) # s.commit() On Mon, Aug 26, 2013 at 5:52 PM, Praveen <praveen.venk...@gmail.com> wrote: > It works only in 0.7 like you said. I can't find any way to crack this > situation in 0.6. :-( > > > On Mon, Aug 26, 2013 at 5:49 PM, Praveen <praveen.venk...@gmail.com>wrote: > >> nvm... i found it. >> >> >> On Mon, Aug 26, 2013 at 5:46 PM, Praveen <praveen.venk...@gmail.com>wrote: >> >>> Could you please point me to the link where I can find the example ? >>> >>> >>> On Mon, Aug 26, 2013 at 5:41 PM, Michael Bayer <mike...@zzzcomputing.com >>> > wrote: >>> >>>> you'd need to hand-roll the deferred reflection part, there's an >>>> example in 0.7 called "declarative_reflection" but it might require >>>> features that aren't in 0.6. >>>> >>>> I'd not be looking to add any kind of slick/magic systems to an 0.6 >>>> app, 0.6 is very early in the curve for declarative techniques. upgrade it >>>> first, otherwise stick with the hacky approaches you have. >>>> >>>> >>>> >>>> On Aug 26, 2013, at 5:38 PM, Praveen <praveen.venk...@gmail.com> wrote: >>>> >>>> Does this work in sqlalchemy 0.6.1 ? >>>> >>>> >>>> On Mon, Aug 26, 2013 at 5:36 PM, Michael Bayer < >>>> mike...@zzzcomputing.com> wrote: >>>> >>>>> OK here we are, had to switch approaches due to a bug with the column >>>>> reflect event, to use the aforementioned __mapper_cls__ (had the name >>>>> wrong), so I think you'll see this is a pretty open-ended way to control >>>>> how something maps as you're given total access to mapper() here: >>>>> >>>>> from sqlalchemy import * >>>>> from sqlalchemy.orm import * >>>>> from sqlalchemy.ext.declarative import declarative_base, declared_attr >>>>> from sqlalchemy.ext.declarative import DeferredReflection >>>>> from sqlalchemy import event >>>>> >>>>> Base = declarative_base() >>>>> >>>>> e = create_engine("sqlite://", echo=True) >>>>> e.execute(""" >>>>> create table sample ( >>>>> Id integer primary key, >>>>> Name varchar, >>>>> Description varchar, >>>>> IsActive varchar >>>>> ) >>>>> """) >>>>> >>>>> class MagicMappyThing(DeferredReflection): >>>>> @declared_attr >>>>> def __mapper_cls__(cls): >>>>> def map_(cls, *arg, **kw): >>>>> props = kw.setdefault("properties", {}) >>>>> for k, v in cls.__map__.items(): >>>>> props[v] = cls.__table__.c[k] >>>>> return mapper(cls, *arg, **kw) >>>>> return map_ >>>>> >>>>> class Sample(MagicMappyThing, Base): >>>>> __tablename__ = 'sample' >>>>> __map__ = {'Id': 'id', 'Name': 'name', 'Description': >>>>> 'description', 'IsActive': 'is_active'} >>>>> >>>>> def __init__(self, id, name, description, is_active): >>>>> self.id = id >>>>> self.name = name >>>>> self.description = description >>>>> self.is_active = is_active >>>>> >>>>> MagicMappyThing.prepare(e) >>>>> >>>>> s = Session(e) >>>>> >>>>> s.add(Sample(id=1, name='some name', description='foo', >>>>> is_active='foo')) >>>>> s.commit() >>>>> >>>>> >>>>> >>>>> >>>>> On Aug 26, 2013, at 5:17 PM, Michael Bayer <mike...@zzzcomputing.com> >>>>> wrote: >>>>> >>>>> >>>>> On Aug 26, 2013, at 5:16 PM, Michael Bayer <mike...@zzzcomputing.com> >>>>> wrote: >>>>> >>>>> >>>>> On Aug 26, 2013, at 4:35 PM, Praveen <praveen.venk...@gmail.com> >>>>> wrote: >>>>> >>>>> The problem with using Mixins is that you need to know the definition >>>>> of columns already for creating the mixin class. What I am trying to do is >>>>> more like get the definition dynamically on the fly.Take a look at this: >>>>> >>>>> >>>>> here's a simple way to add reflection events which intercept the >>>>> Column and add the .key of your choice, and saves on code by making use of >>>>> the existing DeferredReflection mixin instead of hand-coding it. >>>>> >>>>> >>>>> except it doesnt work yet, give me 10 minutes. >>>>> >>>>> >>>>> >>>> >>>> >>>> -- >>>> Have a nice day !!! >>>> >>>> -- >>>> 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 http://groups.google.com/group/sqlalchemy. >>>> For more options, visit https://groups.google.com/groups/opt_out. >>>> >>>> >>>> >>> >>> >>> -- >>> Have a nice day !!! >>> >> >> >> >> -- >> Have a nice day !!! >> > > > > -- > Have a nice day !!! > -- Have a nice day !!! -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
api.py
Description: Binary data