apply_initial_migration fails
See original GitHub issueHi,
I’ve just discovered this package and wanted to test a new migration that I am writing. So far, the project consists of a few apps, and the app I want to write migration tests for has currently > 100 applied migrations.
For the sake of simplicity, I set migrate_from
and migrate_to
to an already applied migration (~101 -> 102, only a change in a field type) just to fiddle arround. Curiously, it failed.
Traceback:
`MySQLdb._exceptions.OperationalError: (3730, "Cannot drop table 'wagtailcore_collectionviewrestriction' referenced by a foreign key constraint 'wagtailcore_collecti_collectionviewrestri_47320efd_fk_wagtailco' on table 'wagtailcore_collectionviewrestriction_groups'.")
env/lib/python3.6/site-packages/MySQLdb/connections.py:239: OperationalError
The above exception was the direct cause of the following exception:
env/lib/python3.6/site-packages/django_test_migrations/contrib/unittest_case.py:36: in setUp
self.migrate_from,
env/lib/python3.6/site-packages/django_test_migrations/migrator.py:46: in apply_initial_migration
sql.drop_models_tables(self._database, style)
env/lib/python3.6/site-packages/django_test_migrations/sql.py:32: in drop_models_tables
get_execute_sql_flush_for(connection)(database_name, sql_drop_tables)
env/lib/python3.6/site-packages/django/db/backends/base/operations.py:405: in execute_sql_flush
cursor.execute(sql)
env/lib/python3.6/site-packages/django/db/backends/utils.py:68: in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
env/lib/python3.6/site-packages/django/db/backends/utils.py:77: in _execute_with_wrappers
return executor(sql, params, many, context)
env/lib/python3.6/site-packages/django/db/backends/utils.py:86: in _execute
return self.cursor.execute(sql, params)
env/lib/python3.6/site-packages/django/db/utils.py:90: in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
env/lib/python3.6/site-packages/django/db/backends/utils.py:84: in _execute
return self.cursor.execute(sql)
env/lib/python3.6/site-packages/django/db/backends/mysql/base.py:74: in execute
return self.cursor.execute(query, args)
env/lib/python3.6/site-packages/MySQLdb/cursors.py:209: in execute
res = self._query(query)
env/lib/python3.6/site-packages/MySQLdb/cursors.py:315: in _query
db.query(q)
and a lot more other errors related to drop
queries. As I understand it, during setup, django-test-migrations
takes the current test_db and deletes all models in the database, and then reapplies all migrations up and including to migrate_from
. In my case, this seems to fail. I cannot imagine what I could have done wrong in my testcase, as the error occurs during the setUp
function of the MigratorTestCase
.
I don’t think that it has anything to do with wagtail, as the testcase seems to fail with different errors on each run:
self = <_mysql.connection closed at 0x35419e8>
query = b'DROP TABLE `auth_group` CASCADE'
def query(self, query):
# Since _mysql releases GIL while querying, we need immutable buffer.
if isinstance(query, bytearray):
query = bytes(query)
> _mysql.connection.query(self, query)
E django.db.utils.OperationalError: (3730, "Cannot drop table 'auth_group' referenced by a foreign key constraint 'auth_group_permissions_group_id_b120cbf9_fk_auth_group_id' on table 'auth_group_permissions'.")
Testcase:
class TestMigrations(MigratorTestCase):
migrate_from = ('myapp', None)
migrate_to = ('myapp', '001_initial')
def prepare(self):
pass
def test_migration001(self):
self.assertTrue(True)
Maybe I am missing something crucial and obvious, so here is what I did:
install django-test-migrations
with pip
wrote a unittest testcase
, as provided in the example (the actual tc is just an assertTrue(True)
ran manage.py test
I did not add anything to INSTALLED_APPS
whatsoever.
Any ideas?
DB: MYSQL 8.0.19 mysqlclient (1.4.6) Django: 3.0.2 django-test-migrations: 1.0.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (1 by maintainers)
Top GitHub Comments
@Hafnernuss thank you for the great and detailed investigation! I will try to fix issues related to MySQL in the following days.
Fix for
django>=3.1
is already onmaster
, but we need to fix a few other issues before releasing the new version.Apparently, the error above is indeed caused by Django 3.1. I have created a MRE that runs on Django 3.0. The problem does not occur when usig SQLITE, however, it does when using MYSQL. The migration does not even have to be applied.
All you have to do is create a env (requirements provided) and change the database credentials in the settings.py file.
The error I recieve:
Tested on python 3.6 and 3.8.
migrate_test.zip