How to list a user's enrollments after you deleted courses

We deleted a course using As explained here, the course is removed, but a lot of data related to the deleted course still exists in databases.

After we removed the course, we tried listing the enrollments for a particular user. This user was previously enrolled on the deleted course, so the user has an entry in the student_courseenrollment table with a course_id matching the deleted course.

The enrollments API GET /api/enrollment/v1/enrollments?username=elena returns a 500 Internal Server Error and the following exception:

Exception Type: AttributeError at /api/enrollment/v1/enrollments
Exception Value: Got AttributeError when attempting to get a value for field `course_id` on serializer `CourseEnrollmentsApiListSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `CourseEnrollment` instance.
Original exception text was: 'NoneType' object has no attribute 'id'.

The workaround for this is to use the enrollment API instead (so enrollment instead of enrollments):
GET /api/enrollment/v1/enrollment?user=elena.

We spent some time figuring this out and hope this helps someone running into the same error.