Error executing request

I get an error message for this request

query = "select count(*) from pgns_game where raw_moves = %s" params = ('a',) total_rows = self.model.objects.raw(query, params) 

and he says

 InvalidQuery('Raw query must include the primary key') 

Something is obviously missing me, but I do not know what. Any ideas?

+4
source share
1 answer

self.model.objects.raw() expects the query result to contain the primary keys from the self.model model, so it can turn them into a list of objects for the function result.

What you really need to do is execute SQL directly, not through the manager. Your code will probably look like this:

 from django.db import connection cursor = connection.cursor() cursor.execute("select count(*) from pgns_game where raw_moves = %s", ['a']) total_rows = cursor.fetchone() 

I have not even tried this myself.

+16
source

Source: https://habr.com/ru/post/1310925/


All Articles