# Django orm make query wrong for Mysql db.

**URL:** <https://forum.djangoproject.com/t/django-orm-make-query-wrong-for-mysql-db/1657>\
**Category:** Using Django\
**Created:** [March 25, 2020, 1:33pm UTC](https://forum.djangoproject.com/t/django-orm-make-query-wrong-for-mysql-db/1657 "2020-03-25T13:33:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahulkoundal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rahulkoundal/32/2262_2.png) [@rahulkoundal](https://forum.djangoproject.com/u/rahulkoundal)\
**Post date:** [March 25, 2020, 1:33pm UTC](https://forum.djangoproject.com/t/django-orm-make-query-wrong-for-mysql-db/1657/1 "2020-03-25T13:33:49Z")

</div>

i have face the problem in django orm  
the field value is — ‘Korea, Republic of’  
but the django orm make query and treat like two value is **‘Korea ‘,’ Republic of’'**  
please help me 🙂

q\_objects= (OR: (‘region\_\_in’, [‘Americas Region’, ‘Europe Region’, ‘Russia Region’, ‘Middle East and Africa Region’]), (‘reg\_temp\_\_in’, [‘Comoros’, “Cote D’Ivoire”, ‘Ghana’, ‘Australia’, ‘China’, ‘Hong Kong’, ‘Indonesia’, ‘Japan’, **‘Korea, Republic of’** , ‘Malaysia’, ‘New Zealand’, ‘Philippines’, ‘Singapore’, ‘Sri Lanka’, ‘Taiwan’, ‘Thailand’, ‘Viet Nam’]))

users = obj\_users.filter(q\_objects)

print(“user query++++++++”, users.query)

query is ---------  
IN (Viet Nam, Malaysia, Hong Kong, Thailand, Japan, Australia, Philippines, New Zealand,  
**Korea, Republic of,** China, Indonesia, Comoros, Taiwan, Cote D’Ivoire, Singapore, Sri Lanka, Ghana)

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 25, 2020, 1:54pm UTC](https://forum.djangoproject.com/t/django-orm-make-query-wrong-for-mysql-db/1657/2 "2020-03-25T13:54:41Z")

</div>

`str(QuerySet.query)` doesn’t show the exact SQL that will be executed (except on PostgreSQL). It is broken for quoting strings. In this case, it looks wrong, but if you actually execute the query, you’ll see it’s correct.

Here’s how to see the actual SQL being executed: [https://docs.djangoproject.com/en/3.0/faq/models/#how-can-i-see-the-raw-sql-queries-django-is-running](https://docs.djangoproject.com/en/3.0/faq/models/#how-can-i-see-the-raw-sql-queries-django-is-running)

---

<div class="post-metadata">

**Author:** ![rahulkoundal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rahulkoundal/32/2262_2.png) [@rahulkoundal](https://forum.djangoproject.com/u/rahulkoundal)\
**Post date:** [March 26, 2020, 4:43am UTC](https://forum.djangoproject.com/t/django-orm-make-query-wrong-for-mysql-db/1657/3 "2020-03-26T04:43:54Z")

</div>

@adamchainz query is not working correct …  
it takes " Koria, Republic" of two values and give wrong results.  
Please help me or suggest me better way

---

<div class="post-metadata">

**Author:** ![kplaube](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kplaube/32/629_2.png) [@kplaube](https://forum.djangoproject.com/u/kplaube)\
**Post date:** [March 26, 2020, 8:54am UTC](https://forum.djangoproject.com/t/django-orm-make-query-wrong-for-mysql-db/1657/4 "2020-03-26T08:54:54Z")

</div>

@rahulkoundal,

@adamchainz has a point, and it’s a surprise that your query is not working. Which database are you using?

Have you tried to use quotes wrapping the string? Like `"'Korea, Republic of'"`?

There is this answer from Stackoverflow that suggests the same: [https://stackoverflow.com/questions/18078542/django-queryset-filter-missing-quotes](https://stackoverflow.com/questions/18078542/django-queryset-filter-missing-quotes)

---

<div class="post-metadata">

**Author:** ![rahulkoundal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rahulkoundal/32/2262_2.png) [@rahulkoundal](https://forum.djangoproject.com/u/rahulkoundal)\
**Post date:** [March 27, 2020, 5:24am UTC](https://forum.djangoproject.com/t/django-orm-make-query-wrong-for-mysql-db/1657/5 "2020-03-27T05:24:52Z")

</div>

@kplaube @adamchainz  
My database is MySql  
`"'Korea, Republic of'"` is not working for me .  
i use also the trick of the stackflow but still is not working. its for Sqlite database only.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 27, 2020, 9:43am UTC](https://forum.djangoproject.com/t/django-orm-make-query-wrong-for-mysql-db/1657/6 "2020-03-27T09:43:35Z")

</div>

It works at query time. But `QuerySet.query` _does not_ show the actual SQL that’s run, but an approximation, hence it’s wrong. I made an example model on MariaDB locally and the query works fine:

```auto
In [1]: from example.core.models import Book

In [2]: Book??
Init signature: Book(*args, **kwargs)
Docstring: Book(id, title)
Source:
class Book(models.Model):
    title = models.CharField(max_length=200)
File: ~/tmp/in-mysql/example/core/models.py
Type: ModelBase
Subclasses:

In [3]: Book.objects.create(title="Korea, Republic of")
Out[3]: <Book: Book object ((1,))>

In [4]: Book.objects.filter(title__in=["Korea, Republic of"])
Out[4]: <QuerySet [<Book: Book object (1)>]>

In [5]: Book.objects.filter(title__in=["Korea, Republic of"]).query
Out[5]: <django.db.models.sql.query.Query at 0x106f11910>

In [6]: str(Book.objects.filter(title__in=["Korea, Republic of"]).query)
Out[6]: 'SELECT `core_book`.`id`, `core_book`.`title` FROM `core_book` WHERE `core_book`.`title` IN (Korea, Republic of)'

```

The linked stack overflow is very old (2013) and I’m not sure it’s applicable any more.
