OK, well theres not too much of a bug here, except that when ActiveMapper has a circular relationship between two classes (comprised of more than one relationship(), that is), it fails to set up the relationships on either class.   theres no immediate fix for this; while the compilation of plain (non-Active) mappers is able to handle a circular relationship like that, the way activemapper compiles on top of it is not quite as flexible.  jonathan and i had talked about a way to add some extra string-based argument capabilities to Mapper/relation() so that a lot more of the compilation code in ActiveMapper could just be removed which would probably eliminate this issue.

however, the reason its not super-critical is because this relationship is a backref relationship, which means youre only supposed to set it up on one class, where  "backref" handles the other direction:

         class foo(activemapper.ActiveMapper):
             class mapping:
                 name = column(String(30))

         class baz(activemapper.ActiveMapper):
             class mapping:
                 name = column(String(30))
                 foorel = many_to_many("foo", secondarytable, backref='bazrel')

additionally, i removed the extra "tometadata" call on "secondarytable" since its not needed, and also re-loaded foo1/baz1 after the session.clear() since it better tests the relatoinship and also foo1/baz1 cant be connected together outside of a session because theres a lazy loader on each that needs to be fired off (play around with it and youll see what i mean).

anyway, the working test added to activemapper.py in rev 1767...thanks for the convenient format.

On Aug 6, 2006, at 5:40 PM, Graham Higgins wrote:


class testmanytomany(testbase.PersistTest):

     def setUpAll(self):

         global secondarytable, foo, baz

         secondarytable = Table("secondarytable",

                                 activemapper.metadata,

                                 Column("foo_id", Integer, ForeignKey 

("foo.id"),primary_key=True),

                                 Column("baz_id", Integer, ForeignKey 

("baz.id"),primary_key=True))


         secondarytable.tometadata(activemapper.metadata)


         class foo(activemapper.ActiveMapper):

             class mapping:

                 name = column(String(30))

                 bazrel = many_to_many('baz', secondarytable,  

backref='foorel')


         class baz(activemapper.ActiveMapper):

             class mapping:

                 name = column(String(30))

                 foorel = many_to_many("foo", secondarytable,  

backref='bazrel')


         activemapper.metadata.connect(testbase.db)

         activemapper.create_tables()


     # Create a couple of activemapper objects

     def create_objects(self):

         return foo(name='foo1'), baz(name='baz1')


     def tearDownAll(self):

         clear_mappers()

         activemapper.drop_tables()


     def testbasic(self):

         # Set up activemapper objects

         foo1, baz1 = self.create_objects()


         objectstore.flush()

         objectstore.clear()


         # Just checking ...

         assert (foo1.name == 'foo1')

         assert (baz1.name == 'baz1')


         # Diagnostics ...

         # import sys

         # sys.stderr.write("\nbazrel missing from dir(foo1):\n%s\n"  

% dir(foo1))

         # sys.stderr.write("\nbazrel in foo1 relations:\n%s\n" %  

foo1.relations)


         # Optimistically based on activemapper one_to_many test, try  

to append

         # baz1 to foo1.bazrel - (AttributeError: 'foo' object has no  

attribute 'bazrel')


         foo1.bazrel.append(baz1)

         assert (foo1.bazrel == [baz1])


-------------------------------------------------------------------------
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
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to