Issue
Tried to updating unqiue_together constraint using Django-ORM
While Migrating getting following error :
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1664, in <module>
main()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/arpitkoolwal/DEV/Gateway-26Oct/gateway/gateway/manage.py", line 24, in <module>
execute_from_command_line(sys.argv)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/arpitkoolwal/DEV/cld/pyenv3/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/arpitkoolwal/DEV/cld/pyenv3/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/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/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/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 536, in database_forwards
getattr(new_model._meta, self.option_name, set()),
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 365, in alter_unique_together
self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/backends/mysql/schema.py", line 88, in _delete_composed_index
return super(DatabaseSchemaEditor, self)._delete_composed_index(model, fields, *args)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 394, in _delete_composed_index
", ".join(columns), ValueError: Found wrong number (0) of constraints for test(a, b)
Django : 1.11.15
Error Ticket : Django-Offical
Can anyone tell me, How can we modify existing unique constraint using Django-ORM ?
EDIT :
Model -
class test(models.Model):
a = models.CharField(max_length=500, blank=True, null=True)
b = models.CharField(max_length=500, blank=True, null=True)
c = models.CharField(max_length=500, blank=True, null=True)
d = models.CharField(max_length=500, blank=True, null=True)
class Meta:
unique_together = ('a', 'b')
Migration Code :
class Migration(migrations.Migration):
dependencies = [
('demo', '0230_auto_20181106_1243'),
]
operations = [
migrations.AlterUniqueTogether(
name='test',
unique_together=set([a,b]),
),
]
Solution
How I solved the problem:
python -m pdb manage.py migrate
type
up
to go upper frame. It will go to./python3.6/site-packages/django/db/backends/base/schema.py(363)alter_unique_together() -> self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
type
a
to show variables
Notice
old_unique_together = {('a', 'b')}
new_unique_together = {('a', 'b', 'c')}
that the old_unique_together
does not exist, according to pg \d tablename
- Go to old migration files and find old
migrations.AlterUniqueTogether
, comment them, then run the migration again. - ???
- profit.
Answered By - est
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.