class_mapper failing with self-referential Declarative

Issue #1344 resolved
Former user created an issue

Here is a traceback: File '/Users/percious/oss/tgtut/src/pycon-tg-tutorial/my-intranet/my_intranet/controllers/root.py', line 62 in <module> class RootController(BaseController): File '/Users/percious/oss/tgtut/src/pycon-tg-tutorial/my-intranet/my_intranet/controllers/root.py', line 77 in RootController admin = Catwalk(model, DBSession) File '/Users/percious/oss/tgtut/lib/python2.5/site-packages/Catwalk-2.0.2-py2.5.egg/catwalk/tg2/controller.py', line 39 in init super(Catwalk, self).init(models, session, config_type=self.config_type) File 'build/bdist.macosx-10.3-i386/egg/tgext/admin/controller.py', line 38 in init File 'build/bdist.macosx-10.3-i386/egg/tgext/admin/config.py', line 85 in init File '/Users/percious/oss/tgtut/lib/python2.5/site-packages/SQLAlchemy-0.5.1-py2.5.egg/sqlalchemy/orm/util.py', line 542 in class_mapper class_manager = attributes.manager_of_class(class_) File '/Users/percious/oss/tgtut/lib/python2.5/site-packages/SQLAlchemy-0.5.1-py2.5.egg/sqlalchemy/orm/attributes.py', line 1643 in manager_of_class finder = self.manager_finderscls File '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/weakref.py', line 243 in getitem return self.dataref(key) TypeError: cannot create weak reference to 'classobj' object

And here is the model which created it: contact_friend_table = Table('friend', metadata, Column('contact_id', Integer, ForeignKey('contact.contact_id')), Column('friend_id', Integer, ForeignKey('contact.contact_id')) )

class Contact(DeclarativeBase): tablename = 'contact'

contact_id = Column(Integer, primary_key = True)
first_name = Column(Unicode(40), nullable=False)
last_name = Column(Unicode(40), nullable=False)
maiden_name = Column(Unicode(40))
address = Column(Unicode(80), nullable=False)
city = Column(Unicode(80), nullable=False)
_state = Column(Unicode(3), ForeignKey('state.state_id'), nullable=False)
state = relation('State')
zipcode = Column(Integer,nullable=False)
birthday = Column(Date())
gender =  Column(Unicode(6))
description = Column(Unicode)
created =  Column(DateTime, default=datetime.now())
friends = relation('Contact', secondary=contact_friend_table, 
                              primaryjoin=contact_id == contact_friend_table.c.contact_id,
                              secondaryjoin=contact_friend_table.c.friend_id == contact_id,
                              foreign_keys = [contact_id](contact_id)
                              )

class_mapper(Contact)

-percious chris@percious.com

Comments (3)

  1. Former user Account Deleted

    Better formatting of the class:

    contact_friend_table = Table('friend', metadata,
        Column('contact_id', Integer, ForeignKey('contact.contact_id')),
        Column('friend_id', Integer, ForeignKey('contact.contact_id'))
    )
    
    class Contact(DeclarativeBase):
        __tablename__ = 'contact'
    
        contact_id = Column(Integer, primary_key = True)
        first_name = Column(Unicode(40), nullable=False)
        last_name = Column(Unicode(40), nullable=False)
        friends = relation('Contact', secondary=contact_friend_table, 
                                      primaryjoin=contact_id == contact_friend_table.c.contact_id,
                                      secondaryjoin=contact_friend_table.c.friend_id == contact_id,
                                      foreign_keys = [contact_id](contact_id)
                                      )
    
  2. Log in to comment