﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33256	Some schema tests don't clean up their tables	Tim Graham	Mohammad Ali Mehdizadeh	"Some test errors on a second consecutive run of `./tests/runtests.py --settings=test_postgres  --parallel=1 --keepdb schema` shows that some tests leave behind their tables. Attached is an initial patch for a couple of the cases.
{{{
======================================================================
ERROR: test_add_foreign_key_quoted_db_table (schema.tests.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation ""table_author_double_quoted"" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/home/tim/code/django/django/test/utils.py"", line 437, in inner
    return func(*args, **kwargs)
  File ""/home/tim/code/django/django/test/testcases.py"", line 1305, in skip_wrapper
    return test_func(*args, **kwargs)
  File ""/home/tim/code/django/tests/schema/tests.py"", line 3229, in test_add_foreign_key_quoted_db_table
    editor.create_model(Author)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 355, in create_model
    self.execute(sql, params or None)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 151, in execute
    cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/utils.py"", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation ""table_author_double_quoted"" already exists


======================================================================
ERROR: test_add_foreign_object (schema.tests.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation ""schema_bookforeignobj"" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/home/tim/code/django/tests/schema/tests.py"", line 3238, in test_add_foreign_object
    editor.create_model(BookForeignObj)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 355, in create_model
    self.execute(sql, params or None)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 151, in execute
    cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/utils.py"", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation ""schema_bookforeignobj"" already exists


======================================================================
ERROR: test_remove_db_index_doesnt_remove_custom_indexes (schema.tests.SchemaTests)
Changing db_index to False doesn't remove indexes from Meta.indexes.
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation ""schema_authorwithindexedname"" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/home/tim/code/django/tests/schema/tests.py"", line 2659, in test_remove_db_index_doesnt_remove_custom_indexes
    editor.create_model(AuthorWithIndexedName)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 355, in create_model
    self.execute(sql, params or None)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 151, in execute
    cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/utils.py"", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation ""schema_authorwithindexedname"" already exists


======================================================================
ERROR: test_remove_field_unique_does_not_remove_meta_constraints (schema.tests.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation ""schema_authorwithuniquename"" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/home/tim/code/django/django/test/testcases.py"", line 1305, in skip_wrapper
    return test_func(*args, **kwargs)
  File ""/home/tim/code/django/tests/schema/tests.py"", line 2057, in test_remove_field_unique_does_not_remove_meta_constraints
    editor.create_model(AuthorWithUniqueName)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 355, in create_model
    self.execute(sql, params or None)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 151, in execute
    cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/utils.py"", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation ""schema_authorwithuniquename"" already exists


======================================================================
ERROR: test_remove_index_together_does_not_remove_meta_indexes (schema.tests.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation ""schema_authorwithindexednameandbirthday"" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/home/tim/code/django/django/test/testcases.py"", line 1305, in skip_wrapper
    return test_func(*args, **kwargs)
  File ""/home/tim/code/django/tests/schema/tests.py"", line 2554, in test_remove_index_together_does_not_remove_meta_indexes
    editor.create_model(AuthorWithIndexedNameAndBirthday)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 355, in create_model
    self.execute(sql, params or None)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 151, in execute
    cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/utils.py"", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation ""schema_authorwithindexednameandbirthday"" already exists


======================================================================
ERROR: test_remove_unique_together_does_not_remove_meta_constraints (schema.tests.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation ""schema_authorwithuniquenameandbirthday"" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/home/tim/code/django/django/test/testcases.py"", line 1305, in skip_wrapper
    return test_func(*args, **kwargs)
  File ""/home/tim/code/django/tests/schema/tests.py"", line 2176, in test_remove_unique_together_does_not_remove_meta_constraints
    editor.create_model(AuthorWithUniqueNameAndBirthday)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 355, in create_model
    self.execute(sql, params or None)
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 151, in execute
    cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/utils.py"", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation ""schema_authorwithuniquenameandbirthday"" already exists


======================================================================
ERROR: test_unique_name_quoting (schema.tests.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.DuplicateTable: relation ""unique-table"" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/home/tim/code/django/tests/schema/tests.py"", line 1978, in test_unique_name_quoting
    editor.alter_db_table(TagUniqueRename, old_table_name, 'unique-table')
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 474, in alter_db_table
    self.execute(self.sql_rename_table % {
  File ""/home/tim/code/django/django/db/backends/base/schema.py"", line 151, in execute
    cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
  File ""/home/tim/code/django/django/db/utils.py"", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/tim/code/django/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation ""unique-table"" already exists
}}}"	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed			Ready for checkin	1	0	0	0	1	0
