Related
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
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
I have no valuable data in my database and was unable to migrate, as you can see here:
ResetDjango postgresql database? flush does not work
I tried to reset the database using:
Delete migration files
Enter psql command prompt. Connect to database. drop schema public cascade; create schema public;
Step 2 unfortunately seemed to have removed my user role and rights so I went back to the psql command promt and recreated those.
Now I have tried to migrate again, and it still doesn't work. What might be wrong now?
vagrant#vagrant-ubuntu-trusty-64:/vagrant/grader$ python3 manage.py makemigrations
Migrations for 'core':
0001_initial.py:
- Create model Student
vagrant#vagrant-ubuntu-trusty-64:/vagrant/grader$ python3 manage.py migrate Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
psycopg2.ProgrammingError: no schema has been selected to create in
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/recorder.py", line 57, in ensure_schema
editor.create_model(self.Migration)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 284, in create_model
self.execute(sql, params or None)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: no schema has been selected to create in
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 89, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 176, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
self.ensure_schema()
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/recorder.py", line 59, in ensure_schema
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (no schema has been selected to create in
)
Adding these two lines in settings.py at the top are important for mysql to connect:
from django.db.backends.mysql.base import DatabaseWrapper
DatabaseWrapper.data_types['DateTimeField']='datetime'
I made some changes to my models and then ran a
python manage.py makemigrations
python manage.py migrate
and I got this traceback:
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: sessions, admin, study, auth, quiz, contenttypes, main
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying quiz.0013_auto_20151005_0644...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 154, in apply_migration
self.recorder.record_applied(migration.app_label, migration.name)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 67, in record_applied
self.migration_qs.create(app=app, name=name)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 348, in create
obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 734, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 762, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 846, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 885, in _do_insert
using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 920, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 974, in execute_sql
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 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
DETAIL: Key (id)=(27) already exists.
I didn't know quite how to interpret the error that the primary key already exists. Strangely enough it added the field into the database because I was seeing it output on my site and when I ran a shell I could see that the fields were added and the default data 'that I chose' was populated.
I decided to run the migration again to see if it would pass after a second time and I got a different traceback...
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: sessions, admin, study, auth, quiz, contenttypes, main
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying quiz.0013_auto_20151005_0644...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/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 398, in add_field
self.execute(sql, params)
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 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "lang1_back_to_choice" of relation "quiz_langpairinstructions" already exists
Now if I try it again I will always get the second traceback. I am wondering what I should do so I can make migrations in the futures. Why am I getting this error if everything appears to have gone smoothly in the db itself?
There is another way. Since you know the last id value in django_migrations table, say it is 100. Then, you need to do this:
ALTER SEQUENCE django_migrations_id_seq RESTART WITH 101;
I overlooked the name of the 'django_migrations_pkey' column and I didn't realize that was in the database. Somehow django was one step behind the actual database and trying to use the primary key (pk=27) instead of the number it was actually on (which should have been 28)
I was unsure about how to change this from within django so instead I just deleted the last column from the database so it was one primary key shorter and on track with django and it started working just fine.
I got this error after I had previously manually tinkered with the django migrations tables entries (after some previously dodgy migrations were added then removed).
This left the django_migrations IDs out of order (so to speak) with the default position of the entry in the django_migrations table.
The issue is that django takes the ID of the last entry and +1 to create a new entry (in table below: 197 + 1 = 198), however as you can see Key (id)=(198) already exists in the table.
187 | foo_app | 0016_auto_20161220_2332 | 2017-01-06 05:22:07.666172+00
198 | bar_app | 0004_auto_20160423_2122 | 2017-01-13 05:38:31.922738+00
197 | baz_app | 0013_auto_20170203_2311 | 2017-02-08 18:50:22.70143+00
As you can see the numbers for the primary key on the left are out of order.
What I did was manually delete the out of order entries in the django_migration then re-add them.
DELETE from django_migrations where name = '0004_auto_20160423_2122';
DELETE from django_migrations where name = '0013_auto_20170203_2311';
INSERT INTO django_migrations VALUES (188, 'baz_app', '0013_auto_20170203_2311', date());
INSERT INTO django_migrations VALUES (189, 'bar_app', '0004_auto_20160423_2122', date());
Also of note is that the migration that I was initially trying to add 'new_app, '0002_auto_00000000_1111' actually modified the new_app table in postgres, however the django_migrations table didn't get updated.
So the second time I tried to re-run the migration 0002_auto_00000000_1111 I got the error:
django.db.utils.ProgrammingError: column "new_app_field" of relation "new_app_new" already exists
My solution above fixed the problem for me.
Simply reindexing the django_migrations table worked for me.
Follow these steps in your terminal
Change user to root
$ sudo su
So that you can then then change user to postgres
$ su postgres
Open postgres terminal client with your db selected
$ psql name_of_your_db
In the postgres client issue the following command
REINDEX TABLE django_migrations;
And then quit the postgres client
\q
I'm currently trying to get familiarized with django and I got stuck with an error I can't fix. My django version is 1.7a2 (from developer).
My model is quite simple:
from django.db import models
class dataset(models.Model):
ID = models.CharField("ID of current dataset", max_length=20, primary_key=True)
date = models.DateTimeField('Date of current dataset')
name = models.CharField("Name of current dataset", max_length=20)
value = models.FloatField("Value of current dataset")
But when I run 'python manage.py syncdb' command for the second time I get this error:
$ python manage.py syncdb
Operations to perform:
Synchronize unmigrated apps: admin, contenttypes, auth, sessions
Apply all migrations: display
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
Applying display.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-trunk/django/core/management/__init__.py", line 427, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/base.py", line 287, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/base.py", line 336, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/base.py", line 531, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/commands/syncdb.py", line 22, in handle_noargs
call_command("migrate", **options)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/__init__.py", line 167, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/base.py", line 336, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/core/management/commands/migrate.py", line 145, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/migrations/executor.py", line 60, in migrate
self.apply_migration(migration, fake=fake)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/migrations/executor.py", line 94, in apply_migration
migration.apply(project_state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/migrations/migration.py", line 97, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/migrations/operations/models.py", line 28, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/schema.py", line 253, in create_model
self.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/schema.py", line 95, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/utils.py", line 77, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/utils.py", line 61, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/utils.py", line 93, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/utils.py", line 61, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django-trunk/django/db/backends/sqlite3/base.py", line 475, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: duplicate column name: ID
Does anyone have any idea what I am doing wrong?
Thanks in advance.
I tried removing the db.sqlite3 file inside my django root directory and it didn't work, but then I removed the content of the /migration/ inside the application folder where my model is and it worked without showing any other error.
It seems to me some kind of bug, since it does not update the migration folder once the model changed...
You cannot create an entry with the name ID.
Event if you remove ID it will show errors.
Here is how I fixed it.
Delete the Migrations Directory
Rename ID to some thing else
python manage.py makemigrations
python manage.py migrate
Hopefully this will fix it.