On Fri, Oct 23, 2009 at 11:03 AM, Brian Neal <bgn...@gmail.com> wrote:
>
> Currently I am keeping an eye on SQL queries by displaying at the
> bottom of my base template sql_queries. I'm seeing some queries that
> look kind of strange to me, and I was wondering if there was a way to
> somehow correlate the queries with my app's Python code.
>
> For example, amongst the SQL queries on one of my pages, I see this
> curious pair:
>
> SELECT (1) AS `a` FROM `forums_topic` WHERE `forums_topic`.`id` = 10
> UPDATE `forums_topic` SET `forum_id` = 4, `name` = Split Test #1,
> `creation_date` = 2009-10-19 21:45:05, `user_id` = 2, `view_count` =
> 28, `sticky` = False, `locked` = False, `post_count` = 7,
> `update_date` = 2009-10-22 20:46:11, `last_post_id` = 34 WHERE
> `forums_topic`.`id` = 10
>
> I *think* these two queries are coming from this code:
>
> topic.view_count += 1
> topic.save()
>
> but I'm not sure. Is there a debugging technique or tool that could
> tell me what line in the user code the SQL came from?

There's nothing built into Django to help out here.

> I'm curious what the SELECT (1) is doing and where it came from. Is
> Django's ORM checking to see if the topic object exists before
> updating it?

Correct. Django does a select to determine if it needs to do an INSERT
or UPDATE when the save() is called. If you use force_insert or
force_update as an argument to save, you can override this behaviour.
Predictably, using these flags forces the use of INSERT or UPDATE in
save(), removing the need for the select but introducing the
possibility of error if the relevant object does (or doesn't) exist.

Yours,
Russ Magee %-)

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

Reply via email to