Heroku: error running django migrations - django

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

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 rest framework authentication database error

I am trying to implement the django rest framework in my current project. The api engine is working with no security. I am trying to follow the steps given in the authentication page of django rest framework to add the authentication.
When I am adding the following section to the settings.py, I don't get any error:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
]
}
but when I am adding 'rest_framework.authtoken' to the INSTALLED_APPS, and run the server,
I get the follwing message:
You have 2 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): authtoken.
Run 'python manage.py migrate' to apply them.
Then I tried running:
python manage.py migrate
and I get the following errors:
(raiotic-venv) username#username-VirtualBox:~/Servers/Repositories/raiotic-venv/raiotic$ python manage.py migrate
Operations to perform:
Apply all migrations: MainApp, admin, auth, authtoken, contenttypes, sessions
Running migrations:
Applying authtoken.0001_initial...Traceback (most recent call last):
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 381, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: attempt to write a readonly database
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/init.py", line 381, in execute_from_command_line
utility.execute()
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/init.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/username/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/username/venv/lib/python3.6/site-packages/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/username/venv/lib/python3.6/site-packages/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/username/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/username/venv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/username/venv/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 307, in create_model
self.execute(sql, params or None)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
cursor.execute(sql, params)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/username/venvlib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/username/venvlib/python3.6/site-packages/django/db/utils.py", line 89, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/username/venvlib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/username/venvlib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 381, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: attempt to write a readonly database
try changing the permissions to the db.sqlite3 file it might be that the mode is read only for the user you are running as having no permissions to write to the file.
do
ls -al db.sqlite3
chmod a+w db.sqlite3
python manage.py migrate

Sqllite and Django shell are not in sync

I have deleted a table from sqllite3 shell and then when I try creating it again by running python manage.py migrate/python manage.py makemigrations table is not created and showing below error. How to re-create the table and make my db and the API is insync. I tried with the python manage.py dbsync also but not worked.
Operations to perform:
Apply all migrations: admin, auth, cgkapp, contenttypes, sessions
Running migrations:
Applying cgkapp.0002_delete_questions...Traceback (most recent call last):
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: cgkapp_questions
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
fake_initial=fake_initial,
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/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/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/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/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 246, in database_forwa
rds
schema_editor.delete_model(model)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 287, in delete_model
super().delete_model(model)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 317, in delete_model
"table": self.quote_name(model._meta.db_table),
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 117, in execute
cursor.execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/shrivatsa555/.virtualenvs/django20/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: cgkapp_questions
Django migrate command try to make all migrations (they hold in migrations directory) which are not done yet.
In here you have "0002_delete_questions" migration file in your migrations directory and when you run the migrate command this migration try to delete questions table from your database but this table is already deleted. So you can handle this in two way
Delete this migration file from migrations directory in your project
run this command python manage.py migrate --fake cgkapp 0002_delete_questions (here 'cgkapp' is your app name and '0002_delete_questions' is your migration file name)

Django - migrate before setting custom user model, error message

I have forgotten to set my custom user model in the settings file and migrate my project first time. Now i am trying to make migrations again after setting my custom user model with errors -> ... django.db.utils.ProgrammingError: relation "custom_auth_customuser" does not exist ...
I've learned that this is a common error message but i could not understand how to solve it. I have no 'migrations' folder anywhere... Where is the schema that i will change manually?
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, taggit
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying admin.0001_initial...Traceback (most recent call last):
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "custom_auth_customuser" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/migrations/executor.py", line 97, in migrate
state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/migrations/executor.py", line 132, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/migrations/executor.py", line 236, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 92, in __exit__
self.execute(sql)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 112, in execute
cursor.execute(sql, params)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/burakk/BurakWorks/Web/VIRTUAL_ENVIRONMENTS/python3.5.0_pikto_web_dj_1_10/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "custom_auth_customuser" does not exist
If I unterstand your problem correctly you should try this steps:
Create a mirgrations folder containing __init__.py in your app
Run makemigrations and migrate

Django migration failing because tables already exist?

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