diff --git a/serveradmin/serverdb/migrations/0016_optional_servertype_for_relation.py b/serveradmin/serverdb/migrations/0016_optional_servertype_for_relation.py new file mode 100644 index 00000000..49945bfc --- /dev/null +++ b/serveradmin/serverdb/migrations/0016_optional_servertype_for_relation.py @@ -0,0 +1,21 @@ +# Generated by Django 3.2.23 on 2023-11-03 08:04 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('serverdb', '0015_attribute_history_field'), + ] + + operations = [ + migrations.RunSQL( + "ALTER TABLE attribute DROP CONSTRAINT IF EXISTS attribute_target_servertype_id_check" + ), + # This is the same as before but without a check for type relation. + migrations.RunSQL( + "ALTER TABLE attribute ADD CONSTRAINT attribute_target_servertype_id_check " + "CHECK((type IN ('domain', 'supernet', 'relation')) = (target_servertype_id IS NOT NULL OR type = 'relation'))" + ), + ] diff --git a/serveradmin/serverdb/models.py b/serveradmin/serverdb/models.py index 06fad557..7a625521 100644 --- a/serveradmin/serverdb/models.py +++ b/serveradmin/serverdb/models.py @@ -591,8 +591,6 @@ class Meta: index_together = [['attribute', 'value']] def save_value(self, value): - target_servertype = self.attribute.target_servertype - try: target_server = Server.objects.get(hostname=value) except Server.DoesNotExist: @@ -600,7 +598,8 @@ def save_value(self, value): 'No server with hostname "{0}" exist.'.format(value) ) - if target_server.servertype != target_servertype: + target_servertype = self.attribute.target_servertype + if target_servertype and target_server.servertype != target_servertype: raise ValidationError( 'Attribute "{0}" has to be from servertype "{1}".' .format(self.attribute, self.attribute.target_servertype)