---
slug: "django-テストで-マイグレーションをキャンセルした時-table-already-exists-で止まらないようにする"
title: "Preventing \"table already exists\" error when canceling migrations in Django tests"
description: "Avoid `table already exists` failures in Django tests when running with `--keepdb` after cancelling migrations — how to forcibly reset the test DB migration state."
url: "https://www.ytyng.com/en/blog/django-テストで-マイグレーションをキャンセルした時-table-already-exists-で止まらないようにする"
publish_date: "2018-09-27T02:37:03Z"
created: "2018-09-27T02:37:03Z"
updated: "2026-05-11T13:10:46.705Z"
categories: ["Django"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/d1ef645109394433a023a9434a760623.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Preventing "table already exists" error when canceling migrations in Django tests

<p>Django==1.9.13<br />Mezzanine==4.3.0</p>
<p>During testing with Django Mezzanine Cartridge, when the migration was canceled</p>
<p>In settings</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #000080; font-weight: bold;">if </span><span style="color: #008080; font-weight: bold;">'test' </span><span style="color: #000080; font-weight: bold;">in </span>sys.argv:<br />    CAPTCHA_TEST_MODE = <span style="color: #000080; font-weight: bold;">True<br /></span><span style="color: #000080; font-weight: bold;">    </span>DATABASES = {<br />        <span style="color: #008080; font-weight: bold;">'default'</span>: {<br />            <span style="color: #008080; font-weight: bold;">'ENGINE'</span>: <span style="color: #008080; font-weight: bold;">'django.db.backends.sqlite3'</span>,<br />            <span style="color: #008080; font-weight: bold;">'NAME'</span>: <span style="color: #008080; font-weight: bold;">':memory:'</span>,<br />        },<br />    }<br />    DATABASE_ROUTERS = []<br /><br /><span style="color: #008080; font-weight: bold;">    </span><span style="color: #000080; font-weight: bold;">class </span>DisableMigrations(<span style="color: #000080;">object</span>):<br />        <span style="color: #000080; font-weight: bold;">def </span><span style="color: #b200b2;">__contains__</span>(<span style="color: #94558d;">self</span>, item):<br />            <span style="color: #000080; font-weight: bold;">return True<br /></span><span style="color: #000080; font-weight: bold;"><br /></span><span style="color: #000080; font-weight: bold;">        def </span><span style="color: #b200b2;">__getitem__</span>(<span style="color: #94558d;">self</span>, item):<br />            <span style="color: #000080; font-weight: bold;">return </span><span style="color: #008080; font-weight: bold;">'notmigrations'<br /></span><span style="color: #008080; font-weight: bold;"><br /></span><span style="color: #008080; font-weight: bold;">    </span>MIGRATION_MODULES = DisableMigrations()</pre>
<p>I was doing it like this,</p>
<p>but due to issues related to the trouth= in ManyToMany fields, the create table was stopped with "table already exists" error and the tests could not proceed.</p>
<p></p>
<p>Therefore, by patching the CREATE TABLE statement of BaseDatabaseSchemaEditor to CREATE TABLE IF NOT EXISTS, you can execute without stopping.</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #000080; font-weight: bold;">from </span>django.db.backends.sqlite3.schema <span style="color: #000080; font-weight: bold;">import </span>DatabaseSchemaEditor<br /><br /><span style="color: #808080; font-style: italic;"># Patch CREATE TABLE<br /></span>DatabaseSchemaEditor.sql_create_table = \<br />    <span style="color: #008080; font-weight: bold;">"CREATE TABLE IF NOT EXISTS %(table)s (</span><span style="color: #660e7a; font-weight: bold;">%(definition)s</span><span style="color: #008080; font-weight: bold;">)"<br /></span>DatabaseSchemaEditor.sql_create_index = \<br />    <span style="color: #008080; font-weight: bold;">"CREATE INDEX IF NOT EXISTS %(name)s ON %(table)s (%(columns)s)%(extra)s"<br /></span>DatabaseSchemaEditor.sql_create_unique = \<br />    <span style="color: #008080; font-weight: bold;">"CREATE UNIQUE INDEX IF NOT EXISTS %(name)s ON %(table)s (%(columns)s)"</span></pre>
<p></p>
<p></p>
<p>It might be a good idea to patch django.db.utils.DatabaseErrorWrapper to suppress the errors.</p>
