#9047 - Fix constraint check

This commit is contained in:
Daniel Sheppard 2023-03-22 08:17:17 -05:00
parent 5fa3146912
commit 1b6e67ba0a
2 changed files with 5 additions and 3 deletions

View File

@ -27,7 +27,7 @@ def create_provideraccounts_from_providers(apps, schema_editor):
# #
def revert_provideraccounts_from_providers(apps, schema_editor): def revert_provideraccounts_from_providers(apps, schema_editor):
ProviderAccount = apps.get_model('circuits', 'ProviderAccount') ProviderAccount = apps.get_model('circuits', 'ProviderAccount')
provideraccounts = ProviderAccount.objects.all().orderby('pk') provideraccounts = ProviderAccount.objects.all().order_by('pk')
for provideraccount in provideraccounts: for provideraccount in provideraccounts:
if provideraccounts.filter(provider=provideraccount.provider)[0] == provideraccount: if provideraccounts.filter(provider=provideraccount.provider)[0] == provideraccount:
provideraccount.provider.account = provideraccount.account provideraccount.provider.account = provideraccount.account
@ -66,7 +66,7 @@ class Migration(migrations.Migration):
), ),
migrations.AddConstraint( migrations.AddConstraint(
model_name='provideraccount', model_name='provideraccount',
constraint=models.UniqueConstraint(fields=('provider', 'account'), name='circuits_provideraccount_unique_provider_account'), constraint=models.UniqueConstraint(condition=models.Q(('account', ''), _negated=True), fields=('provider', 'account'), name='circuits_provideraccount_unique_provider_account'),
), ),
migrations.RunPython( migrations.RunPython(
create_provideraccounts_from_providers, revert_provideraccounts_from_providers create_provideraccounts_from_providers, revert_provideraccounts_from_providers

View File

@ -1,5 +1,6 @@
from django.contrib.contenttypes.fields import GenericRelation from django.contrib.contenttypes.fields import GenericRelation
from django.db import models from django.db import models
from django.db.models import Q
from django.urls import reverse from django.urls import reverse
from netbox.models import PrimaryModel from netbox.models import PrimaryModel
@ -81,7 +82,8 @@ class ProviderAccount(PrimaryModel):
), ),
models.UniqueConstraint( models.UniqueConstraint(
fields=('provider', 'account'), fields=('provider', 'account'),
name='%(app_label)s_%(class)s_unique_provider_account' name='%(app_label)s_%(class)s_unique_provider_account',
condition=~Q(account="")
), ),
) )