youre only allowed to define one primary mapper per class. by having
assign_mapper in the constructor of a class, creating a new mapper for the
Bar class which is in a static context, it creates a new mapper for the
Bar class each time Foo's __init__ is called; all calls to this after the
first will fail. this might have appeared to "work" in 0.1 since it was
making non-primary mappers on all the subsequent calls; since people
frequently get confused by that, you now have to be explicit if you want a
non primary mapper (and I dont think thats what youre looking for here).
basically, in this example youre looking to pull all that code out of
__init__ and put it at the module level, i.e. right after the definition
for Foo is best, and its against "Foo.Bar" which is what "self.Bar" really
is.
POX wrote:
> Hi,
>
> Why undermentioned code will not work with SA 0.2.6 anymore?
>
> After rinning:
>
> | #!/usr/bin/python
> |
> | from sqlalchemy import *
> | from sqlalchemy.mods.threadlocal import assign_mapper
> |
> | class Foo(object):
> | class Bar(object):
> | pass
> | def __init__(self, url):
> | metadata = BoundMetaData(url)
> | table = Table('test', metadata,
> | Column('test_id', Integer),
> | Column('foo', Boolean))
> | assign_mapper(self.Bar, table)
> |
> | tmp = Foo("sqlite://")
> | #del tmp
> | tmp2 = Foo("sqlite://")
>
> I'm getting this:
>
> Traceback (most recent call last):
> File "./test.py", line 18, in ?
> tmp2 = Foo("sqlite://")
> File "./test.py", line 14, in __init__
> assign_mapper(self.Bar, table)
> File "/usr/lib/python2.3/site-packages/sqlalchemy/mods/threadlocal.py",
> line 33, in assign_mapper
> assignmapper.assign_mapper(objectstore.context, class_, *args,
> **kwargs)
> File "/usr/lib/python2.3/site-packages/sqlalchemy/ext/assignmapper.py",
> line 31, in assign_mapper
> m = mapper(class_, extension=extension, *args, **kwargs)
> File "/usr/lib/python2.3/site-packages/sqlalchemy/orm/__init__.py", line
> 49, in mapper
> return Mapper(class_, table, *args, **params)
> File "/usr/lib/python2.3/site-packages/sqlalchemy/orm/mapper.py", line
> 133, in __init__
> self._compile_class()
> File "/usr/lib/python2.3/site-packages/sqlalchemy/orm/mapper.py", line
> 414, in _compile_class
> raise exceptions.ArgumentError("Class '%s' already has a primary
> mapper defined. Use is_primary=True to assign a new primary mapper to
> the class, or use non_primary=True to create a non primary Mapper" %
> self.class_)
> sqlalchemy.exceptions.ArgumentError: Class '<class '__main__.Bar'>'
> already has a primary mapper defined. Use is_primary=True to assign a new
> primary mapper to the class, or use non_primary=True to create a non
> primary Mapper
> --
> -=[ Piotr Ozarowski ]=-
> -=[ http://www.ozarowski.pl ]=-
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share
> your
> opinions on IT & business topics through brief surveys -- and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV_______________________________________________
> Sqlalchemy-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users
>
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users