In my controller I have:
________
query = session.query(Article, Category, User, UserVote)
query = query.outerjoin((UserVote, and_
(Article.id==UserVote.article_id,
UserVote.voter==currently_logged_in_user)))
query = query.outerjoin((Category, Article.category_id ==
Category.id))
query = query.outerjoin((User, Article.submitter_id == User.id))

c.articles = query
________


and then in my template (jinja2) :
________
{% for article, category, submitter, uservote in c.articles %}
{{article.title}} was submitted by {{submitter.name}}, you have voted
for this {{uservote.stars}}
{% endfor %}
________

And this works great. But the problem is that when the user is not
logged in there is obviously no 'currently_logged_in_user'. Looking at
the logger I can see the SQL query ends up like this -

________
LEFT OUTER JOIN uservotes ON articles.id = uservotes.article_id AND
uservotes.voter_id IS NULL
________

So when the user is not logged in, my application trys to select a
uservote where the voter_id is set to null. This will give me the
resulting query I want, because there will never be a uservote record
with the voter_id set to NULL, but it is obviously not the correct way
to do a query. If I was doing the query myself, I would simply not
include the uservote join when the user is not logged in.

If I was to selectively change the query call based on whether the
user is logged in, ie..
________
query = session.query(Article, Category, User, UserVote)
[would become]
query = session.query(Article, Category, User)
________

Then the template, which is expecting 4 items
________
{% for article, category, submitter, uservote in articles %}
________

would get an unpacking error, because it expects the uservote to be
there

So what I am trying to figure out, is if there is a way to have the
query return None in place of the missing UserVote when the user is
not logged in?

I'm hoping there might be some kind of .join_placeholder(UserVote)
method that I have somehow overlooked, or something which can achieve
the same result.

Thanks in advance

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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