Django迁移。如何检查迁移中是否存在表?

时间:2016-11-11 11:12:27

标签: python django postgresql

我目前正在开发基于Django 1.8和Postgres的应用程序。这个应用程序安装在几个环境中,其中一些环境中存在DB中的旧表,我需要从中删除记录。

我使用以下SQL查询编写了迁移:

IF EXISTS (
    SELECT relname FROM pg_class WHERE relname=tablename
) THEN 
    DELETE FROM tablename END IF;

但Django在此查询中抛出错误:

django.db.utils.ProgrammingError: syntax error at or near "IF" 

我可以在迁移中以某种方式检查该表是否存在,然后才执行查询,如DROP FROM tablename

2 个答案:

答案 0 :(得分:1)

使用django.db.connection解决了问题。代码:

from django.db import migrations
from django.db import connection


class Migration(migrations.Migration):
    db_cursor = connection.cursor()
    check_exists_query = "SELECT relname FROM pg_class WHERE relname=%s;"
    base_query = "DELETE FROM {table} WHERE condition;"
    tables = [tables]
    existing_tables = []

    for table in tables:
        db_cursor.execute(check_exists_query, [table])
        result = db_cursor.fetchone()
        if result:
            existing_tables.append(table)

    operations = [
        migrations.RunSQL(base_query.format(table=existing_table)) for existing_table in existing_tables
    ]

答案 1 :(得分:0)

检查表是否存在的最简单方法是使用django.db.connection.introspection.table_names()

from django.db import connection

...

all_tables = connection.introspection.table_names()
old_tables = set('old_table_1', 'old_table_2')
existing_old_tables = old_tables.union(all_tables)
# clean tables in existing_old_tables with migrations.RunSQL() as suggested above