I am trying to use server-side curser in psycop2 as described in this blog post . This is essentially achieved using
from django.db import connection if connection.connection is None: cursor = connection.cursor()
When I execute the request:
cursor.execute('SELECT * FROM %s WHERE foreign_id=%s' % (table_name, id))
I get a ProgrammingError :
psycopg2.ProgrammingError: can't use a named cursor outside of transactions
I naively tried to create a transaction using
cursor.execute('BEGIN')
before executing a SELECT . However, this results in the same error generated from the cursor.execute('BEGIN') .
I also tried using
cursor.execute('OPEN gigantic_cursor FOR SELECT * FROM %s WHERE foreign_id=%s' % (table_name, id))
but I get the same results.
How to make a transaction in django?
source share