2

这是我的第一个问题。

我正在尝试在 django 中执行 SQL 查询(南迁移):

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'])
    # ...

我像这样使用这个功能:

self.transform_id_to_pk('my_table_name')

所以它应该:

  1. 找到最大的现有 ID 或 0(它崩溃)
  2. 创建序列名称
  3. 创建序列
  4. 更新 ID 字段以使用序列
  5. 将 ID 更新为 PK

但它崩溃了,错误说:

  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'"
                              ^

为了方便起见,我缩短了文件路径。

“E'accounting_delegation'”是什么意思?我怎么能摆脱它?

谢谢!

卡洛斯。

4

1 回答 1

4

问题是您对非 SQL 数据的事物使用 DB-API 参数化。当您执行以下操作时:

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

DB-API 模块(在这种情况下,是您使用的任何数据库的 django 前端)将知道适当地转义 'col1' 和 'col2' 的内容,并用它们替换 %s。请注意,%s 周围没有引号。但这仅适用于 SQL数据,不适用于 SQL元数据,例如表名和序列名,因为它们需要以不同方式引用(或根本不引用)。当你这样做时

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

表名被引用,就好像你的意思是它是要插入的字符串数据一样,你最终会得到例如“'table_foo'”。您需要将作为查询一部分的 SQL 元数据与不是查询的 SQL 数据分开,如下所示:

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

请注意,因为 django DB-API 前端的 paramstyle 是 'pyformat'(它使用 %s 作为占位符),所以当您进行字符串格式化以创建要执行的 SQL 时,您需要转义这些参数。请注意,当您从不安全的来源获取表名并且不对其进行验证时,这对于 SQL 注入攻击是不安全的。

于 2010-04-14T11:40:37.537 回答