﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36319	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	irfan-hyperion		"# 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:
1. Update all pending facts to verified status
2. Resequence facts
3. 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
1. When facts are marked as VERIFIED, they should remain in that state
2. The status change should be atomic and consistent
3. No facts should revert back to PENDING status without explicit updates

## Actual Behavior
1. Facts that are set to VERIFIED status sometimes revert back to PENDING
2. This happens without any explicit update queries being triggered
3. The issue appears to be intermittent and not consistently reproducible

## Attempted Solutions
1. Added transaction.atomic() to ensure atomicity
2. Used select_for_update() to prevent race conditions
3. Added detailed logging to track status changes
4. Verified the update was successful by checking the count of verified facts after the update

## Questions
1. Could this be related to Django's transaction management or database isolation levels?
2. Are there any known issues with bulk_update() and status fields?
3. Could this be caused by concurrent requests or race conditions despite our locking?
4. 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:
1. The number of facts being updated
2. The status before and after updates
3. Verification of the update success

## Request for Help
We would appreciate any insights into:
1. Potential causes for this behavior
2. Best practices for handling status transitions in Django
3. Debugging strategies to identify the root cause
4. Any known issues or edge cases with bulk updates and status fields

Thank you for your time and assistance!"	Bug	closed	Database layer (models, ORM)	3.2	Normal	invalid	orm, django, sql, database		Unreviewed	0	0	0	0	0	0
