Spaces:
Sleeping
Sleeping
fix: sync audit log model with database schema
Browse filesROOT CAUSE: Model was out of sync with actual database schema
- Database: created_at is TIMESTAMP WITH TIME ZONE
- Model: Was incorrectly defined as String(50)
- Schema: Expected str but received datetime objects
FIXES:
1. Changed model created_at from String to DateTime(timezone=True)
2. Changed schema created_at from str to datetime
3. Added json_encoders to properly serialize datetime to ISO format
This fixes all 87 validation errors in audit logs endpoint.
src/app/models/audit_log.py
CHANGED
|
@@ -45,7 +45,8 @@ class AuditLog(Base):
|
|
| 45 |
additional_metadata = Column(JSONB, default=dict)
|
| 46 |
|
| 47 |
# Timestamp
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
def __repr__(self):
|
| 51 |
return f"<AuditLog(action='{self.action}', user='{self.user_email}', entity='{self.entity_type}')>"
|
|
|
|
| 45 |
additional_metadata = Column(JSONB, default=dict)
|
| 46 |
|
| 47 |
# Timestamp
|
| 48 |
+
from sqlalchemy import DateTime
|
| 49 |
+
created_at = Column(DateTime(timezone=True), default=datetime.utcnow)
|
| 50 |
|
| 51 |
def __repr__(self):
|
| 52 |
return f"<AuditLog(action='{self.action}', user='{self.user_email}', entity='{self.entity_type}')>"
|
src/app/schemas/audit_log.py
CHANGED
|
@@ -34,10 +34,13 @@ class AuditLogResponse(BaseModel):
|
|
| 34 |
latitude: Optional[str] = None
|
| 35 |
longitude: Optional[str] = None
|
| 36 |
additional_metadata: Optional[Dict[str, Any]] = None
|
| 37 |
-
created_at:
|
| 38 |
|
| 39 |
class Config:
|
| 40 |
from_attributes = True
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
class AuditLogListResponse(BaseModel):
|
|
|
|
| 34 |
latitude: Optional[str] = None
|
| 35 |
longitude: Optional[str] = None
|
| 36 |
additional_metadata: Optional[Dict[str, Any]] = None
|
| 37 |
+
created_at: datetime
|
| 38 |
|
| 39 |
class Config:
|
| 40 |
from_attributes = True
|
| 41 |
+
json_encoders = {
|
| 42 |
+
datetime: lambda v: v.isoformat() if v else None
|
| 43 |
+
}
|
| 44 |
|
| 45 |
|
| 46 |
class AuditLogListResponse(BaseModel):
|