Hi!

I have a tree structure modeled into a database using adjacency-list
paradigm (so having a reference to the parent).

class Node(Base):
  id        = Column(Integer, primary_key=True)
  name      = Column(String(120))
  parent_id = Column(Integer, ForeignKey('id'))

I'm adding some custom relationships like "ancestors", which should give me
the full list of ancestors until the root of the tree.
For implementing this, I am trying to create a viewonly relationship which
shall be empty until I manually load it (because the join conditions are
too difficult to specify at the relationship constructor).
So, I define a "fake" relationship:

  ancestors = relationship(Node, viewonly=True, uselist=False,
lazy="noload")

When I load Node instances from the database, this collection is empty. OK.

But I can force the collection to be loaded using contains_eager. Something
like this:

  Parent = aliased(Node)
  node = session.query(Node)...(complex join
ommited)...options(contains_eager('ancestors', alias=Parent)).one()

Then, if the node WAS NOT on the identity_map, the collection is loaded
successfully. GREAT!

  node.ancestors = InstrumentedList([.........])

But, if the node was already in the identity_map, the collection is not
touched, and remains empty :(

  node.ancestors = InstrumentedList([])

I am beginning to think that this may be the expected behaviour, as when
the new instance is retrieved from the database, if the identity_key is the
same as one from the identity_map, it is returning that instance from the
identity_map, without touching anything. Is it so?

One final comment. With the contains_eager query, the instances that are
part of the collection are retrieved from the database but not stored on
the identity_map. Is that also the expected behaviour, isn't it?

Thank you very much!

-- 
----------------------------------
Pau Tallada Crespí
Dep. d'Astrofísica i Cosmologia
Port d'Informació Científica (PIC)
Tel: +34 93 586 8233
----------------------------------

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