Django migration failing because tables already exist? - django

So I changed some models and the data structure changed dramatically and made a migration fail. I cleared out the tables that were affected in the database, deleted all the migration files and then tried to migrate again.
It made migrations for all the tables (even the ones that I left behind in the database) and when I try to migrate I get this error:
Operations to perform:
Apply all migrations: study
Running migrations:
Rendering model states... DONE
Applying study.0001_initial...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 286, in create_model
self.execute(sql, params or None)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 111, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "study_language" already exists
If I go into the migration files and delete the spots where it is trying to migrate an existing table, I get this error...
Operations to perform:
Apply all migrations: study
Running migrations:
Rendering model states... DONE
Applying study.0001_initial...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 236, in create_model
definition, extra_params = self.column_sql(model, field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 135, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 2003, in db_parameters
return {"type": self.db_type(connection), "check": []}
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1994, in db_type
rel_field = self.related_field
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1897, in related_field
return self.foreign_related_fields[0]
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1631, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1618, in related_fields
self._related_fields = self.resolve_related_fields()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1603, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model u'study.Language' cannot be resolved
How can I get this to work without deleting all the tables I left behind in the db?

What worked for me is that I commented out all of the migrations that were not in the db in models.py. I then cleaned the migration folder again and ran the command...
$ python manage.py makemigrations
$ python manage.py migrate appname --fake-initial
Which went smoothly and then I uncommented the models I wanted to add to the db and ran a normal migration...
$ python manage.py makemigrations
$ python manage.py migrate
Everything went smoothly

Related

Running migrate on postgresql db is attempting to migrate already migrated migrations

I've recently upgraded form sqlite to postgresql for my django project.
In the past, whenever I've made a change to a model, I simply ran makemigrations and then the migrate command. No issues.
Now, with posgresql, when I make the INITIAL makemigrations/migrate, it works fine as per usual, but if I make a change to a model and then run makemigrations and then migrate, on the 'migrate' command, the system tries to apply the initial migration again and returns an error saying the table already exist (which it does).
How can I run migrate without the system trying to remigrate migrations that have already been migrated?
Thanks!
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/jimmy/lib/python3.6/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/jimmy/lib/python3.6/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/jimmy/lib/python3.6/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/jimmy/lib/python3.6/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/jimmy/lib/python3.6/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/jimmy/lib/python3.6/django/core/management/commands/migrate.py", line 233, in handle
fake_initial=fake_initial,
File "/home/jimmy/lib/python3.6/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/jimmy/lib/python3.6/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/jimmy/lib/python3.6/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/jimmy/lib/python3.6/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/jimmy/lib/python3.6/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/home/jimmy/lib/python3.6/django/db/backends/base/schema.py", line 324, in create_model
self.execute(sql, params or None)
File "/home/jimmy/lib/python3.6/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/jimmy/lib/python3.6/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/jimmy/lib/python3.6/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/jimmy/lib/python3.6/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/jimmy/lib/python3.6/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/jimmy/lib/python3.6/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "profiles_accentcolor" already exists
Looks like i have to run python manage.py migrate --fake-initial to ignore initial migrations.

Django migrate command fails with change in foreign key related name

I have a model with a foreign key field. I want to give the foreign key a related name. However, migration fails because of the following error:
IndexError: list index out of range
I'm using Django==1.10.5
Database backend is PostgreSQL
models.py Before/After migrations
Before:
class Contest_participants(models.Model):
contest = models.ForeignKey(Contest)
After:
class Contest_participants(models.Model):
contest = models.ForeignKey(Contest, related_name='contests')
The makemigrations command works fine, but the migrate command fails with the above noted error.
Complete Traceback Error:
Running migrations:
Applying teams.0013_auto_20171031_1225...Traceback (most recent call last):
File "manage.py", line 19, in <module>
execute_from_command_line(sys.argv)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 204, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 495, in alter_field
old_db_params, new_db_params, strict)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py", line 117, in _alter_field
new_db_params, strict,
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 504, in _alter_field
fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 931, in _constraint_names
constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/postgresql/introspection.py", line 178, in get_constraints
"foreign_key": tuple(used_cols[0].split(".", 1)) if kind.lower() == "foreign key" else None,
IndexError: list index out of range
EDIT: Migrations
class Migration(migrations.Migration):
dependencies = [
('teams', '0012_auto_20170904_1524'),
]
operations = [
migrations.AlterField(
model_name='contest_participants',
name='contest',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contests', to='contest.Contest'),
),
]

Migrate command error in Django

I am getting a migrate command error --
django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'module_name' used in key specification without a key length")
The model is
class Coupling(models.Model):
coupling_name = models.CharField(max_length=150, blank=False, default=True, unique=True)
module_name = models.TextField(max_length=255)
There is a key length though.
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\base.py", line 345, in execute
output = self.handle(*args, **options)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
fake_initial=fake_initial,
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\operations\fields.py", line 204, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\base\schema.py", line 495, in alter_field
old_db_params, new_db_params, strict)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\base\schema.py", line 678, in _alter_field
self.execute(self._create_unique_sql(model, [new_field.column]))
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
cursor.execute(sql, params)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
return self.cursor.execute(query, args)
File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\connections.py", line 292, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1061, "Duplicate key name 'accounts_coupling_coupling_name_175325e1_uniq'")
Any help is highly appreciated. Thanks in advance.
The first error might be because you tried to set unique=True for the TextField. This is not possible for MySQL, as noted in the docs.
The second error is because you are trying to create an index that already exists. This could be because you already ran the migration and it failed part way through. MySQL does not run schema changes in a transaction, so changes aren't rolled back if they fail. You could try dropping the index manually, or, if this is a new project, it might be easier to drop the database and start again.

error django.db.utils.ProgrammingError: relation "myApp_audiofile" does not exist when trying to rename Model

I have a model called AudioFile which is used in several ManyToMany and ManyToOne relations. I tried a simple rename migration (as suggested here)
looking like this:
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('myApp', '0004_feedback'),
]
operations = [
migrations.RenameModel('AudioFile', 'MediaFile')
]
But instead of it working a get an error traceback looking like this:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 369, in database_forwards
to_field,
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 515, in alter_field
old_db_params, new_db_params, strict)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
new_db_params, strict,
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 773, in _alter_field
self.execute(self._create_fk_sql(model, new_field, "_fk_%(to_table)s_%(to_column)s"))
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "myApp_audiofile" does not exist
When trying to revert to the previous migration I get the following error:
Operations to perform:
Target specific migration: 0004_feedback, from myApp
Running migrations:
No migrations to apply.
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 186, in handle
changes = autodetector.changes(graph=executor.loader.graph)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/autodetector.py", line 47, in changes
changes = self._detect_changes(convert_apps, graph)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/autodetector.py", line 133, in _detect_changes
self.old_apps = self.from_state.concrete_apps
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/state.py", line 222, in concrete_apps
self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
File "/Users/mac/.virtualenvs/myVenv/lib/python3.6/site-packages/django/db/migrations/state.py", line 295, in __init__
raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field myApp.Feedback.lesson was declared with a lazy reference to 'myApp.audiofile', but app 'myApp' doesn't provide model 'audiofile'.
The field myApp.Playlist.audio_files was declared with a lazy reference to 'myApp.audiofile', but app 'myApp' doesn't provide model 'audiofile'.
The field myApp.PlaylistOrder.file was declared with a lazy reference to 'myApp.audiofile', but app 'myApp' doesn't provide model 'audiofile'.
The field myApp.Progress.lesson was declared with a lazy reference to 'myApp.audiofile', but app 'myApp' doesn't provide model 'audiofile'.
can anyone explain what is happenning here? Renaming models seems to come with a lot of problems.
I pdbed into the point where it crashes. And the SQL statement crashing is:
ALTER TABLE "myApp_playlistorder" ADD CONSTRAINT "myApp_play_file_id_13fdafe4_fk_myApp" FOREIGN KEY ("file_id") REFERENCES "myApp_audiofile" ("id") DEFERRABLE INITIALLY DEFERRED

Heroku: error running django migrations

I'm writing a django app that is currently deployed on heroku. The latest migrations I have made throw an error which I don't understand, see below.
This error does not appear locally, maybe that is because I use sqlite locally and postgres on heroku, but that is only a guess.
I generated the migration locally and pushed it.
I use django 1.10 and a virtual environment with python 2.7.
Here is my terminal ouput when I try to run the migration:
heroku run python manage.py migrate
Running python manage.py migrate on ⬢ limitless-lake-59470... up, run.3202 (Free)
Operations to perform:
Apply all migrations: admin, auth, contenttypes, jobs, sessions
Running migrations:
Applying jobs.0015_auto_20161230_2039...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 204, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 495, in alter_field
old_db_params, new_db_params, strict)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/postgresql/schema.py", line 117, in _alter_field
new_db_params, strict,
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 649, in _alter_field
params,
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 112, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for integer: ""
The error disappeared after clearing the db and applying all migrations from scratch