After removing an attribute from a model (sqlite3):
class Course(models.Model):
Name = models.CharField(max_length=30,unique=True)
Active = models.BooleanField(default=True)
# rubric = models.ForeignKey(Rubric) #removed attribute
def __unicode__(self):
return u'%s' % (self.Name)
I get errors upon using migrate (makemigrations is fine):
django.db.utils.IntegrityError: NOT NULL constraint failed: gbook_course.rubric_id
This field doesn't exist anymore! I don't understand where this is failing, or where to fix it. Re-adding the attribute and using null=True, blank=True and changing the attribute to a CharField or other type also do not fix the problem.
Opening the DB from the sqlite command-line application and looking at the schema for the course table, I get the indication that there's no rubric field there:
CREATE TABLE "gbook_course" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "Active" bool NOT NULL, "Name" varchar(30) NOT NULL UNIQUE);
On a related note, I figured that maybe I had just borked the DB, so I also tried deleting it and the app's migrations (except for __init__.py), which should start the thing over from scratch, but I end up with an error both from the runserver and from the migrations:
django.db.utils.OperationalError: no such table: gbook_student
At this point, there's no DB in existence (totally deleted), so I'm really unsure as to where this error could possibly come from. A solution to either problem is fine - I can re-start with a blank DB, if needed.
migrate traceback:
ltjgates:gbsite jgates$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, gbook, sessions
Running migrations:
Rendering model states... DONE
Applying gbook.0011_course_rubric...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/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 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/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 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/executor.py", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/2.7/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 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
field,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 231, in add_field
self._remake_table(model, create_fields=[field])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 199, in _remake_table
self.quote_name(model._meta.db_table),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 112, in execute
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: gbook_course.rubric_id
Deleted DB traceback (makemigrations):
ltjgates:gbsite jgates$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 353, in execute
self.check()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbsite/urls.py", line 21, in <module>
url(r'^gbook/', include('gbook.urls')),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/urls.py", line 2, in <module>
from . import views
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/views.py", line 12, in <module>
from gbook.forms import *
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/forms.py", line 38, in <module>
class StudentChooseForm(forms.ModelForm):
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/forms.py", line 39, in StudentChooseForm
class Meta:
File "/Volumes/ProfileHD/jgates/Desktop/Gbook/gbsite/gbook/forms.py", line 45, in Meta
'Email': CheckboxSelectMultiple(choices=( (x.id, x.FirstName+" "+x.LastName) for x in CHOICES )),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 1085, in _fetch_all
self._result_cache = list(self.iterator())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: gbook_student
Still not sure of the exact cause, but this worked (in the SQLite command line tool):
sqlite> select * from gbook_course;
1|1|Honors Physics
2|1|Physics
3|1|AP Physics
sqlite> delete from gbook_course;
Now the schema shows up with:
CREATE TABLE "gbook_course" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "Name" varchar(30) NOT NULL UNIQUE, "Active" bool NOT NULL, "rubric_id" integer NULL REFERENCES "gbook_rubric" ("id"));
CREATE INDEX "gbook_course_8980b7ae" ON "gbook_course" ("rubric_id");
Related
while running server its working fine; website working fine; everything is fine. but when I am trying migrate something I am getting error as below. even if I am adding new model and trying to do makemigrations and migrate everything happening and working fine, but migrate shows error as below. couldn't find out the problem.
Operations to perform:
Apply all migrations: admin, auth, contenttypes, pmp, sessions
Running migrations:
Applying pmp.0035_alter_tests_reports_visit...Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: new__pmp_tests_reports.visit_id
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\py\patient_management_project\manage.py", line 22, in <module>
main()
File "E:\py\patient_management_project\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 460, in execute
output = self.handle(*args, **options)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 98, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 290, in handle
post_migrate_state = executor.migrate(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 131, in migrate
state = self._migrate_all_forwards(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 163, in _migrate_all_forwards
state = self.apply_migration(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 248, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\migration.py", line 131, in apply
operation.database_forwards(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\operations\fields.py", line 235, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\schema.py", line 172, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 747, in alter_field
self._alter_field(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\schema.py", line 432, in _alter_field
self._remake_table(model, alter_field=(old_field, new_field))
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\schema.py", line 326, in _remake_table
self.execute(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 192, in execute
cursor.execute(sql, params)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 103, in execute
return super().execute(sql, params)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: new__pmp_tests_reports.visit_id
What im doing is i clone my prod database to sandbox. and I'm getting error while migrate Cause of previously I made charfield to datetime field. And then datetime field to Charfield after few migrations.
So how do I solve below error:
Error :
python manage.py migrate
System check identified some issues:
WARNINGS:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, pro_auth, service_list, sessions, token_blacklist
Running migrations:
Applying pro_auth.0002_alter_userorder_order_booking_time...Traceback (most recent call last):
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.InvalidDatetimeFormat: invalid input syntax for type timestamp with time zone: "2021-10-10 10.00"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "/root/sandbox/sandbox_venv/lib/python3.8/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 "/root/sandbox/sandbox_venv/lib/python3.8/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 "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 244, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 608, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/postgresql/schema.py", line 196, in _alter_field
super()._alter_field(
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 765, in _alter_field
self.execute(
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 145, in execute
cursor.execute(sql, params)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for type timestamp with time zone: "2021-10-10 10.00"
And Here is Migration file 0002_alter_userorder_order_booking_time:
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('pro_auth', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='userorder',
name='order_booking_time',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
I think, you need fake migration.
Try this
./manange.py migrate --fake <app_name_of_userorder_is_inclueded> <last_migration_number_which_synced_current_order_booking_time>
This make skip applying alter table.
You're not passing a valid DateTimeFormat. I think you should write
field=models.DateTimeField(default=django.utils.timezone.now()),
based on what is shown here: Django default=timezone.now() saves records using "old" time
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.
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
Django-based project made use of Django 1.6 and now needs upgrade to 1.9. So i just renamed South's 'migrations' folders to 'south_migrations', created 'migrations' folder with init.py in each app that is in INSTALLED_APPS, upgraded all requirements of a project and made changes to code so it would be Django1.9-compatible. Now i want to create native Django migrations running 'makemigrations' but getting this traceback:
Traceback (most recent call last):
File "/home/nervosa/Apps/pycharm-2016.1/helpers/pycharm/django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "/usr/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/nervosa/DjangoProjects/iticket/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/nervosa/DjangoProjects/iticket/iticket/urls.py", line 7, in <module>
from frontend.views import ReturnPaymentURL
File "/home/nervosa/DjangoProjects/iticket/frontend/views.py", line 19, in <module>
from cases.forms import SelfTrafficForm, SelfStartTrafficForm, PJCForm
File "/home/nervosa/DjangoProjects/iticket/cases/forms.py", line 269, in <module>
class SelfTrafficForm(forms.ModelForm):
File "/home/nervosa/DjangoProjects/iticket/cases/forms.py", line 271, in SelfTrafficForm
counties = [(x.pk, x.county.title()) for x in County.objects.all()]
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/nervosa/DjangoProjects/virtualenvs/iticket_venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "places_county" does not exist
LINE 1: ...."main_attorney_id", "places_county"."ds_id" FROM "places_co...
^
When running for separate app, e.g. 'makemigrations billing' i get just the same output. I'm kinda puzzled because even Django's tables aren't created!!!
What is the best way to overcome this?
Your TrafficForm is doing something it shouldn't, which is querying the County objects at import time. While you had an existing database, this was OK(*), but now that you're starting from scratch it will complain.
You should change that form to use ModelChoiceField.
(*) it's almost always a bad idea to do this anyway, because new objects won't be visible in that field until the process is restarted; but in your case I guess new County objects are not often created.