Hi there,

I'm trying to do something pretty weird. I have SQLAlchemy instrumented 
classes that extend a class in addition to Base, like so:

class Region(Base, t_Region):
    __tablename__ = 'region'
    id = Column(
        mysql.INTEGER(11),
        primary_key=True,
        nullable=False,
        autoincrement=True,
        index=True,
        unique=True)
    name = Column(
        mysql.VARCHAR(64),
        nullable=False,
        index=True,
        default='',
        unique=True)

This works great for reads because I can return it anywhere a t_Region is 
expected and everything does what it is supposed to.

But now I can't figure out how to do the reverse; for example, I have a 
t_Region object, and I want to insert it into the database. So far I've got 
this:

mapped = Region(**region.__dict__)

This works, but it lacks elegance as it creates another object.

I also tried region.__class__ = Region, but I got a (obvious) error message:

sqlalchemy.orm.exc.UnmappedInstanceError: Class 
'ame.serf_service.models.region.Region' is mapped, but this instance lacks 
instrumentation.  This occurs when the instanceis created before 
sqlalchemy.orm.mapper(ame.serf_service.model
s.region.Region) was called.

I've tried a few attempts at calling mapper() that haven't been fruitful 
yet. Is there an elegant way to do this?

Bonus points if it can also handle the recursive case, eg:

class A(Base, t_A):
    b = Column(mysql.VARCHAR(20), ForeignKey('b.b'))
    b_obj = relationship('B', foreign_keys=[b])

such that if I get a t_A that has a b_obj of type t_B, t_A and t_B will 
both get mapped appropriately. I'm happy to do this parsing myself, though.

Thanks for any hints,

Dusty

-- 
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/d/optout.

Reply via email to