On Aug 28, 2010, at 1:58 PM, AdriĆ  Mercader wrote:

> Hi everybody,
> I'm using SQLAlchemy for the first time, and everything worked as a
> charm until I got stuck with a double join.
> That's the expression I'm trying to perform to get a list of the
> linked articles from a given article, (knowing its title):
> 
> SELECT a2.id,a2.title
> FROM articles a1 JOIN links l
>    ON a1.id = l.id_parent
>        JOIN articles a2 ON a2.id = l.id_child
> WHERE a1.title = 'Alabama'
> 
> I have defined  the two classes declaratively as follows:
> 
> class Article(Base):
>    __tablename__ = 'articles'
>    id = Column(Integer, primary_key=True)
>    title = Column(Unicode, nullable=False)
> 
> class Link(Base):
>    __tablename__ = 'links'
>    id_parent = Column(Integer, ForeignKey("articles.id"),
> primary_key=True)
>    id_child = Column(Integer, ForeignKey("articles.id"),
> primary_key=True)
> 
> But when I try to execute the following code:
> 
> links = session.query(Article).join((Link,Article.id ==
> Link.id_parent)).join((Article,Article.id == Link.id_child)).all()
> 
> fails with the following error:
> (ProgrammingError) table name "articles" specified more than once
> 
> I don't know if the problem it's in the model definition or in the
> query code, so any help will be greatly appreciated.


you need to use aliased() - see the ORM tutorial section "Using Aliases" for an 
example.


> 
> Thanks,
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to