A few things:

1. the Python dict class cannot be mapped.  Classes can only extend from 
"object" or other classes that in turn extend from "object".

2. SQLAlchemy instrumentation relies upon Python descriptors (see 
http://docs.python.org/howto/descriptor.html) to intercept changes in state on 
an object, that is, setting and getting attributes.   So techniques which 
involve direct access to self.__dict__ will fail to work correctly with a 
mapped class.

3. The mapping of a class is considered to be an integral part of that class' 
definition.   It's not a valid use case to map classes at some arbitrary point 
in time after many objects of that class have been created.  It's for this 
reason that modern SQLAlchemy strongly recommends the use of the Declarative 
pattern introduced at http://docs.sqlalchemy.org/en/latest/orm/tutorial.html as 
the primary means of mapping classes to database tables; it eliminates the 
confusion that the mapping and the class itself can be generally treated as two 
independent things.  While classes can be mapped and unmapped, this is not a 
regular operation and isn't appropriate except in special testing circumstances.


On Feb 22, 2012, at 6:20 AM, Andrea wrote:

> Hi all,
> I have some object on a pre-existing model. Now we want to add a
> persistance layer and so SQLAlchemy/SQLite will be our choice.
> When I add an object to session a UnmappedInstanceError is raised:
> Class 'try_sqlalchemy.example2.applib_model.DescriptorBean'
> is mapped, but this instance lacks instrumentation.  This occurs when
> the instance
> is created before
> sqlalchemy.orm.mapper(try_sqlalchemy.example2.applib_model.DescriptorBean)
> was called.
> 
> This is the example, three code snippets from three python modules:
> http://pastebin.com/KLFFN3ke
> 
> On debug I see that the DescriptorBean instance is created after the
> mapping (mapping is created on startup.createSchema method) so I don't
> understand the error message.
> The same problems if I use declarative and inherit from Base (class
> DescriptorBean(Base, _Struct)). No problems if I have a class that
> inherits directly from object. Maybe is _Struct inheritance that
> destroys mapper instrumentation? Any suggestion?
> 
> Thanks,
> Andrea
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" 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/sqlalchemy?hl=en.
> 

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

Reply via email to