On 4/18/15 7:13 PM, Michael Wilson wrote:
I have the following tables:

things_table = Table(’thing', self.metadata,
        Column('id', Integer, primary_key=True),
…
)

comments_table = Table('comments', self.metadata,
Column('id', Integer, primary_key=True),      # Unique id for this comment
Column('type', Integer), # Type of comment (feedback, etc)
…
)

(And the corresponding mapping).

I’m trying to construct a query like this:

    clauseList  = []
    clauseList.append(Look.creation >= start_date_rounded)
    clauseList.append(Look.creation <= end_date)
    clauseList.append(Look.like_count > 0)

    clauseList.append(Comment.creation >= start_date_rounded)
    clauseList.append(Comment.creation <= end_date)
    clauseList.append(Comment.type == CommentTypeLike)
    clauseList.append(Comment.target_id == Look.id)
    condition = and_(*clauseList)

    looks = session.query(Look, Comment,
func.count(Comment.type)).\
            group_by(Look.id).\
order_by(func.count(Comment.type).desc()).\
            filter(condition).\
            offset(0).\
            limit(count).\
            all()

This fails with :
FROM comments, things WHERE comments.target_id = things.id AND comments.type = :type_1' returned no FROM clauses due to auto-correlation; specify correlate(<tables>) to control correlation manually. The “comments_table” and “things_table” declaration aren’t visible to the function generating the query, but even if I make them visible, and specify :
correlate(things, comments).\
It still fails.
How can I make this work?

by "work" we'd need to know what SQL you are going for. The query(Look, Comment, func.count(Comment.type)) seems very odd because if you are using aggregates in your query, SQL dictates (unless you're using MySQL's cheater mode) that all the other columns that aren't aggregates need to be in the GROUP BY. Also I don't see any subqueries here so nothing that would refer to correlation or produce that message, don't see what CommentTypeLike is, etc.



--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to