5 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: self-referential tab...
FromSent OnAttachments
Steve Zatz27 Jan 2008 17:06 
Michael Bayer27 Jan 2008 22:28 
Steve Zatz28 Jan 2008 03:45 
jason kirtland28 Jan 2008 03:49 
Steve Zatz28 Jan 2008 06:17 
Subject:[sqlalchemy] Re: self-referential table question
From:jason kirtland (je@discorporate.us)
Date:01/28/2008 03:49:23 AM
List:com.googlegroups.sqlalchemy

Steve Zatz wrote:

I realize this is actually an SQL question but I haven't been able to figure out the answer.

In a simple self-referential table, the following produces all the Nodes that are parents to some child node(s):

node_table_alias = node_table.alias() parents = session.query(Node).filter(Node.id == node_table_alias.c.parent_id)

I can't figure out the analogous query that produces all the Nodes that are not parents to another node. It is clear that:

non_parents = session.query(Node).filter(Node.id != node_table_alias.c.parent_id)

doesn't work but I can't figure out what the right query is. Any help would be appreciated.

another option is:

.query(Node).filter(not_(Node.id.in_(select([Node.parent_id]))))