From a225c1784103b45525f9dba3531acfc7337a791e Mon Sep 17 00:00:00 2001 From: ValueOn AG Date: Thu, 6 Nov 2025 13:59:22 +0100 Subject: [PATCH] float dates safety net for different databases --- modules/connectors/connectorDbPostgre.py | 38 +++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/modules/connectors/connectorDbPostgre.py b/modules/connectors/connectorDbPostgre.py index 78ab5e36..cfc37b73 100644 --- a/modules/connectors/connectorDbPostgre.py +++ b/modules/connectors/connectorDbPostgre.py @@ -565,9 +565,24 @@ class DatabaseConnector: record = dict(row) fields = _get_model_fields(model_class) - # Parse JSONB fields back to Python objects + # Ensure numeric fields are properly typed and parse JSONB fields for field_name, field_type in fields.items(): - if ( + # Ensure numeric fields (float/int) are properly typed + # psycopg2 may return them as strings in some environments (e.g., Azure PostgreSQL) + if field_type in ("DOUBLE PRECISION", "INTEGER") and field_name in record: + value = record[field_name] + if value is not None: + try: + if field_type == "DOUBLE PRECISION": + record[field_name] = float(value) + elif field_type == "INTEGER": + record[field_name] = int(value) + except (ValueError, TypeError): + # If conversion fails, log warning but keep original value + logger.warning( + f"Could not convert {field_name} to {field_type} for record {recordId}: {value}" + ) + elif ( field_type == "JSONB" and field_name in record and record[field_name] is not None @@ -843,11 +858,26 @@ class DatabaseConnector: cursor.execute(query, where_values) records = [dict(row) for row in cursor.fetchall()] - # Handle JSONB fields for all records + # Handle JSONB fields and ensure numeric types are correct fields = _get_model_fields(model_class) for record in records: for field_name, field_type in fields.items(): - if field_type == "JSONB" and field_name in record: + # Ensure numeric fields (float/int) are properly typed + # psycopg2 may return them as strings in some environments (e.g., Azure PostgreSQL) + if field_type in ("DOUBLE PRECISION", "INTEGER") and field_name in record: + value = record[field_name] + if value is not None: + try: + if field_type == "DOUBLE PRECISION": + record[field_name] = float(value) + elif field_type == "INTEGER": + record[field_name] = int(value) + except (ValueError, TypeError): + # If conversion fails, log warning but keep original value + logger.warning( + f"Could not convert {field_name} to {field_type} for record {record.get('id', 'unknown')}: {value}" + ) + elif field_type == "JSONB" and field_name in record: if record[field_name] is None: # Convert None to appropriate default based on field name if field_name in [