Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 731335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:00:53+00:00 2026-05-14T07:00:53+00:00

this is my first question. I’m trying to execute a SQL query in django

  • 0

this is my first question.

I’m trying to execute a SQL query in django (south migration):

from django.db import connection
# ...
class Migration(SchemaMigration):
    # ...
    def transform_id_to_pk(self, table):
        try:
            db.delete_primary_key(table)
        except:
            pass
        finally:
            cursor = connection.cursor()

            # This does not work
            cursor.execute('SELECT MAX("id") FROM "%s"', [table])

            # I don't know if this works.
            try:
                minvalue = cursor.fetchone()[0]
            except:
                minvalue = 1
            seq_name = table + '_id_seq'

            db.execute('CREATE SEQUENCE "%s" START WITH %s OWNED BY "%s"."id"', [seq_name, minvalue, table])
            db.execute('ALTER TABLE "%s" ALTER COLUMN id SET DEFAULT nextval("%s")', [table, seq_name + '::regclass'])
            db.create_primary_key(table, ['id'])
    # ...

I use this function like this:

self.transform_id_to_pk('my_table_name')

So it should:

  1. Find the biggest existent ID or 0 (it crashes)
  2. Create a sequence name
  3. Create the sequence
  4. Update the ID field to use sequence
  5. Update the ID as PK

But it crashes and the error says:

  File "../apps/accounting/migrations/0003_setup_tables.py", line 45, in forwards
    self.delegation_table_setup(orm)
  File "../apps/accounting/migrations/0003_setup_tables.py", line 478, in delegation_table_setup
    self.transform_id_to_pk('accounting_delegation')
  File "../apps/accounting/migrations/0003_setup_tables.py", line 20, in transform_id_to_pk
    cursor.execute(u'SELECT MAX("id") FROM "%s"', [table.encode('utf-8')])
  File "/Library/Python/2.6/site-packages/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "E'accounting_delegation'" does not exist
LINE 1: SELECT MAX("id") FROM "E'accounting_delegation'"
                              ^

I have shortened the file paths for convenience.

What does that “E’accounting_delegation'” mean? How could I get rid of it?

Thank you!

Carlos.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:00:54+00:00Added an answer on May 14, 2026 at 7:00 am

    The problem is that you’re using DB-API parameterization for things that are not SQL data. When you do something like:

    cursor.execute('INSERT INTO table_foo VALUES (%s, %s)', (col1, col2))
    

    the DB-API module (django’s frontend for whatever database you are using, in this case) will know to escape the contents of ‘col1’ and ‘col2’ appropriately, and replace the %s’s with them. Note that there are no quotes around the %s’s. But that only works for SQL data, not for SQL metadata, such as table names and sequence names, because they need to be quoted differently (or not at all.) When you do

    cursor.execute('INSERT INTO "%s" VALUES (%s, %s)', (tablename, col1, col2))
    

    the tablename gets quoted as if you mean it to be string data to insert, and you end up with, for example, “‘table_foo'”. You need to separate your SQL metadata, which is part of the query, and your SQL data, which is not, like so:

    sql = 'INSERT INTO TABLE "%s" VALUES (%%s, %%s)' % (tablename,)
    cursor.execute(sql, (col1, col2))
    

    Note that because the django DB-API frontend’s paramstyle is ‘pyformat’ (it uses %s for placeholders) you need to escape those when you do the string formatting to create the SQL you want to execute. And note that this isn’t secure against SQL injection attacks when you take the tablename from an insecure source and don’t validate it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 444k
  • Answers 444k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I figured it out ... I should not be creating… May 15, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer An instance method is only available on an instance of… May 15, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer You can combine an INSERT with a SELECT statement, passing… May 15, 2026 at 6:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.