# django.db.utils.IntegrityError: NOT NULL constraint failed: myapp_order.user_id

- **ID:** `python/django-foreign-key-null-error`
- **Domain:** python
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Trying to save an object with a ForeignKey field set to None, but the field is not nullable.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   Ensure the ForeignKey is set before saving: order.user = request.user
   ```
2. **** (90% success)
   ```
   Use get_or_create to handle missing related objects.
   ```

## Dead Ends

- **** — Allows null values, but may not be intended and can cause data integrity issues. (80% fail)
- **** — Silently fails, data not saved, and may cause inconsistencies. (90% fail)
