Hi!

I try to clean up my code, and read in google group about possibility
create relation to self when class is not defined yet. I write
example,
but got an error (see below).

My code is:
================ cut ====================
engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base(bind=engine)

class Node(Base):
    __tablename__ = 'node'
    id = Column(Integer, primary_key=True)
    title = Column(String(64))
    node_id = Column(Integer, ForeignKey('node.id'))
    children = relation('Node', \
            backref=backref('parent', remote_side='Node.id'))

Base.metadata.create_all()

Session = scoped_session( \
            sessionmaker(autocommit=False, \
                autoflush=False, bind=engine))
session = Session()

root = Node(title='root')
node = Node(title='node')
root.children.append(node)

session.save(root)
session.commit()
================ cut ====================

Workaround worked correctly:

class Node(Base):
  # define attrs without `children'
  # ...

Node.__mapper__.add_property('children', relation(Node, \
            backref=backref('parent', remote_side=Node.id)))


Last traceback lines (formated):

...
File "...orm/properties.py", line 578, in do_init
  self._determine_local_remote_pairs()
File "...orm/properties.py", line 820, in
_determine_local_remote_pairs
  self.local_side, self.remote_side = \
    [util.OrderedSet(x) for x in zip(*list(self.local_remote_pairs))]

ValueError: need more than 0 values to unpack

(self.local_remote_pairs is `[]' here)


Can I define relation inside non-defined class, or I must use some
workarounds?


Thank you.

--~--~---------~--~----~------------~-------~--~----~
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