Skip to content

Commit 5d8a04e

Browse files
committed
cleanup
1 parent a8f0c95 commit 5d8a04e

File tree

2 files changed

+0
-20
lines changed

2 files changed

+0
-20
lines changed

netbox_custom_objects/migrations/0002_ensure_fk_constraints.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
# Generated migration to ensure FK constraints for existing OBJECT fields
2-
31
from django.db import migrations
42

53

64
def ensure_existing_fk_constraints(apps, schema_editor):
75
"""
86
Go through all existing CustomObjectType models and ensure FK constraints
97
are properly set for any OBJECT type fields.
10-
11-
This is needed because the _ensure_fk_constraints method was refactored to work
12-
on individual fields rather than all fields, and this migration ensures existing
13-
fields have proper CASCADE constraints.
148
"""
159
# Import the actual model class (not the historical version) to access methods
1610
from netbox_custom_objects.models import CustomObjectType
1711

1812
for custom_object_type in CustomObjectType.objects.all():
1913
try:
20-
# Get the dynamically generated model for this CustomObjectType
2114
model = custom_object_type.get_model()
22-
23-
# Use the _ensure_all_fk_constraints method which processes all OBJECT fields
24-
# This method is kept specifically for migration purposes
2515
custom_object_type._ensure_all_fk_constraints(model)
2616
except Exception as e:
27-
# Log but don't fail the migration if a specific type has issues
2817
print(f"Warning: Could not ensure FK constraints for {custom_object_type}: {e}")
2918

3019

netbox_custom_objects/models.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,6 @@ def _ensure_all_fk_constraints(self, model):
602602
Ensure that foreign key constraints are properly created at the database level
603603
for ALL OBJECT type fields with ON DELETE CASCADE.
604604
605-
NOTE: This method is deprecated for normal use - use _ensure_field_fk_constraint
606-
for individual fields. This is kept for migration purposes only.
607-
608605
:param model: The model to ensure FK constraints for
609606
"""
610607
# Query all OBJECT type fields for this CustomObjectType
@@ -1558,14 +1555,9 @@ def save(self, *args, **kwargs):
15581555
schema_editor.alter_field(model, old_field, model_field)
15591556

15601557
# Ensure FK constraints are properly created for OBJECT fields with CASCADE behavior
1561-
# Only do this when:
1562-
# 1. Creating a new OBJECT field, OR
1563-
# 2. Updating an existing field where type changed to OBJECT, OR
1564-
# 3. Updating an existing OBJECT field where related_object_type changed
15651558
should_ensure_fk = False
15661559
if self.type == CustomFieldTypeChoices.TYPE_OBJECT:
15671560
if self._state.adding:
1568-
# New OBJECT field - ensure FK constraint
15691561
should_ensure_fk = True
15701562
else:
15711563
# Existing field - check if type changed to OBJECT or related_object_type changed
@@ -1585,7 +1577,6 @@ def save(self, *args, **kwargs):
15851577
super().save(*args, **kwargs)
15861578

15871579
# Ensure FK constraints AFTER the transaction commits to avoid "pending trigger events" errors
1588-
# We use transaction.on_commit() to ensure all deferred constraints are checked first
15891580
if should_ensure_fk:
15901581
def ensure_constraint():
15911582
self.custom_object_type._ensure_field_fk_constraint(model, self.name)

0 commit comments

Comments
 (0)