Dave
Dave

Reputation: 19110

How do I fix my Django "ValueError: Found wrong number (0) of constraints" migration error?

I'm using Python 3.7 and Django. I tried this solution -- Received "ValueError: Found wrong number (0) of constraints for ..." during Django migration, but still got the same error. I'm having trouble with one of my migrations. I recently updated my unique constraint of my model ...

class ArticleSumStatByHour(models.Model):
    total_score = models.DecimalField(default=0, max_digits=12, decimal_places=2, null=False)
    total_seconds_to_reach_fp = models.DecimalField(default=0, max_digits=12, decimal_places=2, null=False)
    num_articles = models.IntegerField(default=0, null=False)
    hour_of_day = IntegerField(
        null=False,
        validators=[
            MaxValueValidator(23),
            MinValueValidator(0)
        ]
    )
    index = models.FloatField(default=0)
    website = models.ForeignKey(website, on_delete=models.CASCADE, related_name='articlesumstatbyhoursub')

        class Meta:
            unique_together = ("hour_of_day","website")

This is my migration,

...
class Migration(migrations.Migration):

    dependencies = [
        ('articlesum', '0032_auto_20190808_1452'),
    ]

    operations = [
        migrations.AlterField(
            model_name='articlesumstatbyhour',
            name='website',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='articlesumstatbyhoursub', to='articlesum.website'),
        ),
        migrations.AlterUniqueTogether(
            name='articlesumstatbyhour',
            unique_together={('hour_of_day','website')},
        ),
      ]

but when I run the migration I get this baffling error complaining about "ValueError: Found wrong number (0) of constraints" ...

(venv) localhost:articlesum_project davea$ python manage.py migrate articlesum
Operations to perform:
  Apply all migrations: articlesum
Running migrations:
  Applying articlesum.0033_auto_20190830_1128...Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/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 "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/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 "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/migrations/operations/models.py", line 514, in database_forwards
    getattr(new_model._meta, self.option_name, set()),
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 356, in alter_unique_together
    self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 385, in _delete_composed_index
    ", ".join(columns),
ValueError: Found wrong number (0) of constraints for articlesum_articlesumstatbyhour(website_id, elapsed_time_in_seconds, hour_of_day)

Why is the migration failing to create my unique constraint?

Upvotes: 1

Views: 3061

Answers (1)

Jolly
Jolly

Reputation: 314

It seems the current migration is looking for the previous index which your database doesn't currently have. Therefore you will need the db table with the last unique index set on it.

Every time an index is altered on a table it checks its previous index and drops it. In your case it is not able to fetch the previous index.

Solution- 1.Either you can generate it manually 2.Or revert to code where previous index is used and migrate.Then finally change to new index in your code and run migration.(django_migration files to be taken care of)

Upvotes: 1

Related Questions