[Django] #31167: Unable to delete or modify a constraint using model Meta

19 views
Skip to first unread message

Django

unread,
Jan 14, 2020, 10:07:31 AM1/14/20
to django-...@googlegroups.com
#31167: Unable to delete or modify a constraint using model Meta
-------------------------------------+-------------------------------------
Reporter: BITSOLVER | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 2.2
Migrations |
Severity: Normal | Keywords: Migrations, unique
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/31167>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 14, 2020, 10:41:41 AM1/14/20
to django-...@googlegroups.com
#31167: Unable to delete or modify a constraint using model Meta
------------------------------------+--------------------------------------
Reporter: BITSOLVER | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 2.2
Severity: Normal | Resolution:
Keywords: Migrations, unique | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------

Comment (by BITSOLVER):

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?

--
Ticket URL: <https://code.djangoproject.com/ticket/31167#comment:1>

Django

unread,
Jan 15, 2020, 2:01:12 AM1/15/20
to django-...@googlegroups.com
#31167: Unable to delete or modify a constraint using model Meta.
------------------------------------+--------------------------------------
Reporter: BITSOLVER | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: worksforme
Keywords: Migrations, unique | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed
* type: Uncategorized => Bug
* version: 2.2 => master
* resolution: => worksforme


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/31167#comment:2>

Reply all
Reply to author
Forward
0 new messages