1

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 ?

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

So i can use Parameterized queries anywhere else ?
Thank you, i'll look into it later.
Can I know the usecase? You could get data from the requests and use this setup to achieve the result you want.
just experimenting, nothing serious :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.