Django DO
Django DO

Reputation: 177

Django/Postgres: Parameterized Queriy does not work

i have just started diving into raw queries in django.

i am trying to change a schema name in my postgres, but my SQL query does not work

here's the code that i run in django shell:

>>> from django.db import connection
>>> cursor = connection.cursor()
>>> query = "ALTER SCHEMA %s RENAME TO %s;"
>>> data = ('thor', 'thor1')
>>> cursor.execute(query, data)

Error:

django.db.utils.ProgrammingError: syntax error at or near "'thor'"
LINE 1: ALTER SCHEMA 'thor' RENAME TO 'thor1';

i believe that those quotes are the root of my problem.

any idea how can i make this work ?

Upvotes: 1

Views: 89

Answers (1)

frozenOne
frozenOne

Reputation: 554

Can't use query parameters here as they'll add single quotes which are not supported by postgres.

Try:

>>> from django.db import connection
>>> cursor = connection.cursor()
>>> data = ('thor', 'thor1')
>>> query = """ALTER SCHEMA %s RENAME TO %s;""" % data
>>> cursor.execute(query)

Upvotes: 1

Related Questions