Database problem in Django

29 views
Skip to first unread message

Ashu Singh

unread,
Jun 18, 2014, 8:03:01 AM6/18/14
to django...@googlegroups.com
Hello everyone. My doubt is how to fetch the recent two database entries in django. User may enter into database anytime but the query should always fetch last two entries. 

Ilya Kazakevich

unread,
Jun 18, 2014, 8:14:56 AM6/18/14
to django...@googlegroups.com
You probably need to have "insert_date" field and store entry created date there.


Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"
>--
>You received this message because you are subscribed to the Google Groups
>"Django users" group.
>To unsubscribe from this group and stop receiving emails from it, send an email to
>django-users...@googlegroups.com.
>To post to this group, send email to django...@googlegroups.com.
>Visit this group at http://groups.google.com/group/django-users.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/django-users/bdd16e14-ef35-4125-966d-60
>6d36f93fd0%40googlegroups.com
><https://groups.google.com/d/msgid/django-users/bdd16e14-ef35-4125-966d-6
>06d36f93fd0%40googlegroups.com?utm_medium=email&utm_source=footer> .
>For more options, visit https://groups.google.com/d/optout.


Glen Jungels

unread,
Jun 18, 2014, 8:31:58 AM6/18/14
to django...@googlegroups.com

It will be different depending on the database (assuming you are asking about the query only).  You will need to do an order by on either your auto incremented field or dat field plus something like a limit 2 (Postgres and I believe MySQL) or top 2 for something like SQL Server.

Erik Cederstrand

unread,
Jun 18, 2014, 9:33:53 AM6/18/14
to Django Users
Den 18/06/2014 kl. 14.31 skrev Glen Jungels <glenj...@gmail.com>:

> It will be different depending on the database (assuming you are asking about the query only). You will need to do an order by on either your auto incremented field or dat field plus something like a limit 2 (Postgres and I believe MySQL) or top 2 for something like SQL Server.

Interestingly, the question is very simple, but the answer is very complicated if you simply *must* get the exact answer.

Databases don't make any promises on the order of the rows returned unless a specific order clause is provided. auto_increment sequences can be reset and primary keys can be reused, so you can't necessarily order by primary key. Result limits are implemented very differently depending on the database vendor since LIMIT is not part of the SQL standard (see http://en.wikipedia.org/wiki/Select_(SQL)#Result_limits), and your database backend might not even support it.

Finally, using an 'insert_date' field only works if the precision of the column type is significantly lower than the rate of insertion. E.g. MySQL <= 5.5 only has second precision for the DATETIME type and wouldn't be useful if you have 10 inserts per second.

Erik

Glen Jungels

unread,
Jun 18, 2014, 9:42:44 AM6/18/14
to django...@googlegroups.com

Erik is certainly right on this.  All queries are point in time and in databases with large numbers of inserts per second, getting the true last two inserts will be relative to when the query starts.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages