@@ -563,48 +563,40 @@ def _ensure_fk_constraints(self, model):
563563 with connection .cursor () as cursor :
564564 for field in object_fields :
565565 field_name = field .name
566- try :
567- model_field = model ._meta .get_field (field_name )
568- if not (hasattr (model_field , 'remote_field' ) and model_field .remote_field ):
569- continue
570-
571- # Get the referenced table
572- related_model = model_field .remote_field .model
573- related_table = related_model ._meta .db_table
574- column_name = model_field .column
575-
576- # Drop existing FK constraint if it exists
577- # Query for existing constraints
578- cursor .execute ("""
579- SELECT constraint_name
580- FROM information_schema.table_constraints
581- WHERE table_name = %s
582- AND constraint_type = 'FOREIGN KEY'
583- AND constraint_name LIKE %s
584- """ , [table_name , f"%{ column_name } %" ])
585-
586- for row in cursor .fetchall ():
587- constraint_name = row [0 ]
588- cursor .execute (f'ALTER TABLE "{ table_name } " DROP CONSTRAINT IF EXISTS "{ constraint_name } "' )
589-
590- # Create new FK constraint with ON DELETE CASCADE
591- constraint_name = f"{ table_name } _{ column_name } _fk_cascade"
592- cursor .execute (f"""
593- ALTER TABLE "{ table_name } "
594- ADD CONSTRAINT "{ constraint_name } "
595- FOREIGN KEY ("{ column_name } ")
596- REFERENCES "{ related_table } " ("id")
597- ON DELETE CASCADE
598- DEFERRABLE INITIALLY DEFERRED
599- """ )
600-
601- except Exception as e :
602- # Log the error but continue with other fields
603- import logging
604- logger = logging .getLogger ('netbox.custom_objects' )
605- logger .warning (f"Failed to ensure FK constraint for { table_name } .{ field_name } : { e } " )
566+ model_field = model ._meta .get_field (field_name )
567+ if not (hasattr (model_field , 'remote_field' ) and model_field .remote_field ):
606568 continue
607569
570+ # Get the referenced table
571+ related_model = model_field .remote_field .model
572+ related_table = related_model ._meta .db_table
573+ column_name = model_field .column
574+
575+ # Drop existing FK constraint if it exists
576+ # Query for existing constraints
577+ cursor .execute ("""
578+ SELECT constraint_name
579+ FROM information_schema.table_constraints
580+ WHERE table_name = %s
581+ AND constraint_type = 'FOREIGN KEY'
582+ AND constraint_name LIKE %s
583+ """ , [table_name , f"%{ column_name } %" ])
584+
585+ for row in cursor .fetchall ():
586+ constraint_name = row [0 ]
587+ cursor .execute (f'ALTER TABLE "{ table_name } " DROP CONSTRAINT IF EXISTS "{ constraint_name } "' )
588+
589+ # Create new FK constraint with ON DELETE CASCADE
590+ constraint_name = f"{ table_name } _{ column_name } _fk_cascade"
591+ cursor .execute (f"""
592+ ALTER TABLE "{ table_name } "
593+ ADD CONSTRAINT "{ constraint_name } "
594+ FOREIGN KEY ("{ column_name } ")
595+ REFERENCES "{ related_table } " ("id")
596+ ON DELETE CASCADE
597+ DEFERRABLE INITIALLY DEFERRED
598+ """ )
599+
608600 def create_model (self ):
609601 from netbox_custom_objects .api .serializers import get_serializer_class
610602 # Get the model and ensure it's registered
0 commit comments