The column_prefix wasn't being honored when the mapper checked for
existing names, this is fixed in trunk r5839.
But also, don't create instances of the object before the class is mapped.
In particular, it's bad form to create tables and mappers inside of class
methods. Create class-level constructs like tables and mappers at the
module level, in the same scope in which you create your classes.
>
> mf2 = Foo.Foo() # ok, let's create a new one.
> # AttributeError: 'Foo' object has no attribute
> '_sa_instance_state'
> myfoo._phone = '555-1212' #
>
> if __name__ == '__main__':
> orm.clear_mappers()
> f = Foo.Foo()
> fs = FooStore()
> fs.map(f)
don't create instances of the object before the class is mapped.
>
> Thanks. I don't have control over the instance creation. These pre-
> created objects are handed down to me. I could create an adapter that
> maps the original class to the one that has been mapped but this is
> quite a bit of work as I have to manually copy over each of the source
> class attributes to my mapped class. I could be clever and perhaps
> look for common attribute names via the internal dictionary but this
> too seems clunky.
>
> I would think this is a common problem (augmenting a class to dump
> it's contents to a db but keep the original class untouched (sort of
> like shelve - but w/o the
> restore capability)). Is there a better way to handle this?
Unfortunately the usage of an object relational mapper assumes that
some degree of convention can be applied to the classes being mapped.
For example, you can't map "old style" classes nor can you map most
natively-backed classes (like cElementTree structures),
instrumentation will fail if the class relies upon direct __dict__
access, and there's probably many other examples of limitations
here. Applying instrumentation to a class to which you don't have
control over is generally a bad idea no matter what persistence
approach you're using, as even if you do get it working, changes to
the implementation of those objects will break your application. So
in this case you definitely should be marshalling these external
objects immediately into an object structure which you have control
over. You also should most certainly be using standard Python
idioms like dir(object), getattr() and possibly __dict__ access in
order to marshall the state of the incoming objects into your own
model using a reflective approach rather than hardcoding every
attribute.