Opened 4 years ago

Closed 4 years ago

#31167 closed Bug (worksforme)

Unable to delete or modify a constraint using model Meta.

Reported by: BITSOLVER Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords: Migrations, unique
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have the following model in a "venue" app on my project:

class StandardCommunityCentrePriceList(models.Model):

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["customer_type", "customer"],
                name="unique_standard_community_price_list"
            )
        ]

    customer_type = models.ForeignKey("customer.CustomerType", null=True, related_name="+", on_delete=CASCADE, blank=True)
    customer = models.ForeignKey("customer.Customer", null=True, related_name="+", on_delete=CASCADE, blank=True)
    price_type = models.CharField(max_length=6, null=False, choices=STANDARD_COMMUNITY_CENTRE_PRICE_TYPE_CHOICES)
    description = models.CharField(max_length=80, null=False)
    tax_inclusive = models.BooleanField(verbose_name=_("Tax Inclusive"), null=False,
                                        default=False)  # False means charges are net of tax, True means charges are inclusive of tax

    @property
    def is_hourly(self):
        return self.price_type == STANDARD_COMMUNITY_CENTRE_PRICE_HOURLY

    @property
    def is_fixed(self):
        return self.price_type == STANDARD_COMMUNITY_CENTRE_PRICE_FIXED

    def clone_price_list(self, source_id):
        source_price_list = StandardCommunityCentrePriceList.objects.get(id=source_id)
        for price_range in source_price_list.ranges.all():
            cloned_range = StandardCommunityCentrePriceRange.objects.create(
                price_list=self,
                effective_from_date=price_range.effective_from_date,
                start_date=price_range.start_date,
                end_date=price_range.end_date
            )
            cloned_range.clone_prices(price_range)

    def __str__(self):
        return "%s" % self.description

When I change the constraint to:

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["customer_type", "customer", "price_type"],
                name="unique_standard_community_price_list"
            )
        ]

I run makemigrations venue and get:

Output:

Migrations for 'venue':
  venue/migrations/0010_auto_20200114_1457.py
    - Remove constraint unique_standard_community_price_list from model standardcommunitycentrepricelist
    - Create constraint unique_standard_community_price_list on model standardcommunitycentrepricelist

Process finished with exit code 0

The migration file contains:

# Generated by Django 2.2.9 on 2020-01-14 14:57

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('venue', '0009_auto_20190930_1440'),
    ]

    operations = [
        migrations.RemoveConstraint(
            model_name='standardcommunitycentrepricelist',
            name='unique_standard_community_price_list',
        ),
        migrations.AddConstraint(
            model_name='standardcommunitycentrepricelist',
            constraint=models.UniqueConstraint(fields=('customer_type', 'customer', 'price_type'), name='unique_standard_community_price_list'),
        ),
    ]

But when I run migrate I get the following error:

Operations to perform:
  Apply all migrations: _holidaypark, _kennels, accommodation, admin, auth, auth_ext, avalonimport, bookings, breeds, captcha, configuration, contact, contenttypes, core, countries, customer, django_celery_beat, django_celery_results, emailer, entitymapper, eventlog, extras, holiday_dates, invoicing, ipn, notes, pets, register_customer, resources, sessions, statuses, supplier, task_ext, tax, venue
Running migrations:
  Applying venue.0010_auto_20200114_1457...Traceback (most recent call last):
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedObject: constraint "unique_standard_community_price_list" of relation "venue_standardcommunitycentrepricelist" does not exist


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/steven/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/193.5662.61/PyCharm 2019.3 EAP.app/Contents/plugins/python/helpers/pycharm/django_manage.py", line 59, in <module>
    run_command()
  File "/Users/steven/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/193.5662.61/PyCharm 2019.3 EAP.app/Contents/plugins/python/helpers/pycharm/django_manage.py", line 46, in run_command
    run_module(manage_file, None, '__main__', True)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/steven/PycharmProjects/bookingsmanager/BookingsManager/src/developer_manage.py", line 28, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/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 "/Users/steven/.virtualenvs/bookingsmanager-3.6/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 "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/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 "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 862, in database_forwards
    schema_editor.remove_constraint(model, constraint)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 351, in remove_constraint
    self.execute(sql)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/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 "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/steven/.virtualenvs/bookingsmanager-3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: constraint "unique_standard_community_price_list" of relation "venue_standardcommunitycentrepricelist" does not exist

I am using Postgresql 9.6.10 and DJango 2.2.9 with psycopg2 2.8.4 and django-pg-utils 0.1.5.

Change History (2)

comment:1 by BITSOLVER, 4 years ago

On further investigation a previous migration had:

# Generated by Django 2.2.5 on 2019-09-30 13:40

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('venue', '0008_standardcommunitycentreprice_venues'),
    ]

    operations = [
        migrations.RemoveConstraint(
            model_name='standardcommunitycentrepricelist',
            name='unique_standard_community_price_list',
        ),
        migrations.AddConstraint(
            model_name='standardcommunitycentrepricelist',
            constraint=models.UniqueConstraint(fields=('customer_type', 'customer'), name='unique_standard_community_price_list'),
        ),
        migrations.RemoveField(
            model_name='standardcommunitycentrepricelist',
            name='code',
        ),
        migrations.AlterField(
            model_name='standardcommunitycentreprice',
            name='day_of_week',
            field=models.IntegerField(choices=[(None, 'Any'), (1, 'Monday'), (2, 'Tuesday'), (3, 'Wednesday'), (4, 'Thursday'), (5, 'Friday'), (6, 'Saturday'), (7, 'Sunday')], null=True),
        ),
    ]

And if I delete remove constraint from the latest migration then run migrate then the migrations completes.

If I then run makemigrations venue again it creates a remove constraint, run again then it creates and add migration back.

Everything is back in sync now, I just wonder if something odd went on with the order of operations for remove & add unique constraints?

comment:2 by Mariusz Felisiak, 4 years ago

Resolution: worksforme
Status: newclosed
Summary: Unable to delete or modify a constraint using model MetaUnable to delete or modify a constraint using model Meta.
Type: UncategorizedBug
Version: 2.2master

Thanks for this ticket, however I'm not able to reproduce this issue, everything works for me.

Note: See TracTickets for help on using tickets.
Back to Top