diff --git a/posthog/warehouse/api/saved_query.py b/posthog/warehouse/api/saved_query.py index 41454f3ae530d4..db437861f967a5 100644 --- a/posthog/warehouse/api/saved_query.py +++ b/posthog/warehouse/api/saved_query.py @@ -80,19 +80,22 @@ def create(self, validated_data): return view def update(self, instance: Any, validated_data: Any) -> Any: - view: DataWarehouseSavedQuery = super().update(instance, validated_data) + with transaction.atomic(): + view: DataWarehouseSavedQuery = super().update(instance, validated_data) - try: - view.columns = view.get_columns() - view.external_tables = view.s3_tables - except Exception as err: - raise serializers.ValidationError(str(err)) + try: + view.columns = view.get_columns() + view.external_tables = view.s3_tables + except RecursionError: + raise serializers.ValidationError("Model contains a cycle") + + except Exception as err: + raise serializers.ValidationError(str(err)) - with transaction.atomic(): view.save() try: - DataWarehouseModelPath.objects.create_from_saved_query(view) + DataWarehouseModelPath.objects.update_from_saved_query(view) except Exception: logger.exception("Failed to update model path when updating view %s", view.name)