Django migrations. How to check if table exists in migrations?
<p>I'm currently working on app built on Django 1.8 and Postgres. This app is installed in several environments, in some of them there old tables in DB from which i need to delete records.</p>

<p>I wrote migration with following SQL query:</p>

<pre><code>IF EXISTS (
    SELECT relname FROM pg_class WHERE relname=tablename
) THEN 
    DELETE FROM tablename END IF;
</code></pre>

<p>But Django throws error at this query:</p>

<pre><code>django.db.utils.ProgrammingError: syntax error at or near "IF" 
</code></pre>

<p>Can i somehow check, in migration, that table exists, and only then execute query, like <code>DROP FROM tablename</code> ?</p>