Closed truhlik closed 8 years ago
Hi @truhlik , This appears to be an issue related to prefetch_relations
function which we are using to reduce the number of queries. It is trying to prefetch more than 61 inter-related tables.
A possible fix would be to either not use prefetched queries or somehow limit the number of tables prefetched.
Both of them sounds a reasonable fix to me. What do you think? What approach should be followed?
cc: @fgmacedo
limit the number of tables prefetched
... seems to me like better approach, but also more complicated. So if you figure out how it could be acomplished, then +1 from me.
not use prefetched queries
Tmp fix until above will be done...?
Yes, it is little complicated but doable.
I am not the original author of that function but from what i can see, stopping select_related()
from being added to the queryset after a limit of tables specified in DB specs is hit will fix the issue.
I'll push a temporary fix where you can turnoff prefetched queries using a setting as soon as i get time. And will work on a permanent fix for that as well.
Thanks.
Great thank you. Let me know if I can help you with anything.
@truhlik Please include the code where your are using the template tag in the base.html
template. I think that you are using the user_notifications
template tag, that is a shortcut to the more generic render_notifications template tag, so you can specify a the queryset to be used. The user_notifications
templatetag uses this query: user.notifications.active().prefetch()
.
You can replace a call to the template tag {% user_notifications %}
, by using something like:
{% render_notifications using user.notifications.active() %}
Please note that you may still try to do some sort of prefetch, as so many joins will slow down your render time doing many queries to fetch related objects.
@fgmacedo 's solution is relevant too.
BTW, @fgmacedo should I still push a commit to let users to decide whether to prefetch by default or not? I think adding that option would be better because we use prefetch()
by default in our views and templatetags.
@v1k45 I don't think that It's an issue. @truhlik just reached the limit of his database backend, PostgreSQL for example, does not have this limit. We can point this on docs, but I think that the workaround above should work.
Thanks @fgmacedo . I'll keep this issue open to take @truhlik 's input.
Give me a little time please. I deleted all records from DB. The issue is not appearing now (with original code). When it appears I will try to change the code. I let you know. Thank you.
Nice. I'll close this issue now. Feel free to comment further if you see this issue again.
Hi guys,
I found related problem with view "notifications" which should display all notifications. There is
notification_list = request.user.notifications.active().prefetch()
I can't obviously fix this in the template. Do you have any idea how to work around this? Temporarly I created my own function but it is not good solution to the future. Any update of your app will affect my project.
Thanks for advice.
I think a simple switch for turning off prefetchs will do. I'll push a fix as soon as i get time.
PS: ignore the old comment, I didn't read your comment completely.
Hi,
during my development after couple dozens of notification suddenly I was getting this exception. Have you ever met with this problem?