Opened 5 months ago
Closed 5 months ago
#36319 closed Bug (invalid)
facing issue where when i change a model field from PENDING to VERIFIED for some reason on next set of code some VERIFIED are changing back to PENDING
Reported by: | irfan-hyperion | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.2 |
Severity: | Normal | Keywords: | orm, django, sql, database |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
# Issue: Unstructured Facts Reverting from VERIFIED to PENDING Status Unexpectedly
## Problem Description
In our Django application, we're experiencing an issue where UnstructuredFact
objects that are set to STATUS_VERIFIED
are unexpectedly reverting back to STATUS_PENDING
. This occurs in the SubmitOverviewPageApiView
class where we handle the verification of meeting notes and their associated facts.
## Environment
- Django version: 3.2.25
- Database: PostgreSQL
- Python version: 3.11.0
## Code Context
The issue occurs in the SubmitOverviewPageApiView.post()
method where we:
- Update all pending facts to verified status
- Resequence facts
- Update meeting review flow progress
## Current Implementation
`python
with transaction.atomic():
# Update all visible facts to VERIFIED status
pending_facts = list(UnstructuredFact.objects.filter(
meeting_note=meeting_note,
status=UnstructuredFact.STATUS_PENDING,
deleted=False,
).select_for_update())
# Update status in memory
for fact in pending_facts:
fact.status = UnstructuredFact.STATUS_VERIFIED
# Bulk save all updated facts
if pending_facts:
UnstructuredFact.objects.bulk_update(pending_facts, status)
`
## Expected Behavior
- When facts are marked as VERIFIED, they should remain in that state
- The status change should be atomic and consistent
- No facts should revert back to PENDING status without explicit updates
## Actual Behavior
- Facts that are set to VERIFIED status sometimes revert back to PENDING
- This happens without any explicit update queries being triggered
- The issue appears to be intermittent and not consistently reproducible
## Attempted Solutions
- Added transaction.atomic() to ensure atomicity
- Used select_for_update() to prevent race conditions
- Added detailed logging to track status changes
- Verified the update was successful by checking the count of verified facts after the update
## Questions
- Could this be related to Django's transaction management or database isolation levels?
- Are there any known issues with bulk_update() and status fields?
- Could this be caused by concurrent requests or race conditions despite our locking?
- Are there any best practices for handling status transitions in Django that we might be missing?
## Additional Context
- The application uses multiple databases (default and legacy)
- We're using Django's ORM for all database operations
- The issue occurs in a production environment with concurrent users
- We've verified that no other parts of the code are explicitly setting the status back to PENDING
## Relevant Models
`python
class UnstructuredFact(models.Model):
STATUS_PENDING = 'PENDING'
STATUS_VERIFIED = 'VERIFIED'
STATUS_DELETED = 'DELETED'
status = models.CharField(
max_length=20,
choices=[
(STATUS_PENDING, 'Pending'),
(STATUS_VERIFIED, 'Verified'),
(STATUS_DELETED, 'Deleted'),
],
default=STATUS_PENDING
)
# ... other fields
`
## Logging Output
We've added detailed logging that shows:
- The number of facts being updated
- The status before and after updates
- Verification of the update success
## Request for Help
We would appreciate any insights into:
- Potential causes for this behavior
- Best practices for handling status transitions in Django
- Debugging strategies to identify the root cause
- Any known issues or edge cases with bulk updates and status fields
Thank you for your time and assistance!
Hello,
This report seems better suited to be a support request. The best place to get answers to your issue is using any of the user support channels from this link.
Since the goal of this issue tracker is to track issues about Django itself, and your issue seems, at first, to be located in your custom code, I'll be closing this ticket as invalid following the ticket triaging process. If, after debugging, you find out that this is indeed a bug in Django, please re-open with the specific details and please be sure to include a small Django project to reproduce or a failing test case.
Thank you!