Django 1.9.8 with mysql 'Commands out of sync; you can't run this command now' exception

583 views
Skip to first unread message

th...@codeographer.net

unread,
Sep 23, 2018, 11:55:49 PM9/23/18
to Django users

I am a python developer and I am using Django 1.9.8 for my project.
I am having a problem with MySQL like below:

I have a model named 'Message' and sometimes when running this code, I got the exception 'Commands out of sync; you can't run this command now'

latest_message = Message.objects.order_by("-id").filter(example_field=1).first()

can you point out to me why does it happen and how to fix it?
thanks,
Thanh Tran

Adolfo Cueto

unread,
Sep 24, 2018, 12:13:14 AM9/24/18
to django...@googlegroups.com
Hey man try to change you code line like this....

latest_message = Message.objects.filter(example_field=1).order_by("-id").first()

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/740fac2d-3399-45d3-bd96-54a76e7f2335%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

th...@codeographer.net

unread,
Sep 24, 2018, 12:41:37 AM9/24/18
to Django users
Thank you so much Adolfo. I know your code is the best practice but are you sure your code will solve the problem?

Adolfo Cueto

unread,
Sep 24, 2018, 12:59:02 AM9/24/18
to django...@googlegroups.com
i know it, that problem ocurred when mysql is being used by other process... so you can add  CONN_MAX_AGE: 0 in your conection string... and also try to use a diferent database and avoid to use multi threading processes

mottaz hejaze

unread,
Sep 24, 2018, 1:09:17 AM9/24/18
to django...@googlegroups.com
try

latest_message = Message.objects.filter(example_field=1)[::-1].first()

--
Reply all
Reply to author
Forward
0 new messages