Merge pull request #60 from valueonag/int
float dates safety net for different databases
This commit is contained in:
commit
84e31a180c
1 changed files with 34 additions and 4 deletions
|
|
@ -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 [
|
||||
|
|
|
|||
Loading…
Reference in a new issue