I have a raw SQL query that I am trying to run in Django. When I display the RawQuerySet object, it's showing the correct query but it isn't returning any output.
I have tried converting the parameters to string and have tried appending quotes to the parameters but that didn't work.
I have also tried the same query but I hardcoded the parameters. That worked.
I opened the dbshell as well to try and see if the query returns an output. It works fine too.
This is what I ran in my dbshell:
select id FROM recommender_item WHERE
id in (select item_id from
recommender_item_likes where user_id = 1)
and color = 'Black';
Note, that the below query did not work:
select id FROM recommender_item WHERE
id in (select item_id from
recommender_item_likes where user_id = 1)
and color = Black;
This is the actual query I want to run:
Item.objects.raw('select id FROM recommender_item WHERE
id in (select item_id from recommender_item_likes where
user_id = %s) and %s = %s', [request.user.id, user_pref, pref_choice,])
This is the same query with hardcoded parameters which is working:
Item.objects.raw('select id FROM recommender_item WHERE
id in (select item_id from recommender_item_likes where user_id = %s)
and color = "Black"', [request.user.id])
The output in my template should be just this list of ids: 1, 64, 437, 1507, 1685
However, right now it just returns []
This is the RawQuerySet object in both cases, respectively:
<RawQuerySet: select id FROM recommender_item WHERE
id in (select item_id from recommender_item_likes where user_id = 1)
and color = Black>
and
<RawQuerySet: select id FROM recommender_item WHERE
id in (select item_id from recommender_item_likes where user_id = 1)
and color = "Black">
Actual SQL query being executed, retrieved from Django debug toolbar:
select id FROM recommender_item WHERE
id in (select item_id from recommender_item_likes where
user_id = '1') and '''color''' = '''"Black"'''
models.py
class Item(models.Model):
#id = models.UUIDField(primary_key = True, default = uuid.uuid4, help_text = 'Unique ID for this particular item')
item_type = models.CharField(max_length = 200, null = True, blank = True)
price = models.CharField(max_length = 200, null = True, blank = True)
color = models.CharField(max_length = 200, null = True, blank = True)
image_URL = models.CharField(max_length = 1000, null = True, blank = True)
fit = models.CharField(max_length = 200, null = True, blank = True)
occasion = models.CharField(max_length = 200, null = True, blank = True)
brand = models.CharField(max_length = 200, null = True, blank = True)
pattern = models.CharField(max_length = 200, null = True, blank = True)
fabric = models.CharField(max_length = 200, null = True, blank = True)
length = models.CharField(max_length = 200, null = True, blank = True)
likes = models.ManyToManyField(User, blank = True, related_name = 'item_likes')
RawQuerySet.__repr__is misleading. If you inspect the actual queries made (usingdjango.db.connection.queries), you should find that contrary to RawQuerySet output, string paramters are quoted in the SQL sent to the database. Not sure what this means in your case though; it seems that if your hardcoded query works, so should the parametrized one.select id FROM recommender_item WHERE id in (select item_id from recommender_item_likes where user_id = '1') and '''color''' = '''"Black"'''I don't know if this will help though.WHERE color = 'black'(DDT shows triple instead of single quotes though). I'm using Django 2.1.7; maybe there were problems in earlier versions. What is your version?