Skip to content

Conversation

@fallenbagel
Copy link
Collaborator

@fallenbagel fallenbagel commented Jan 29, 2026

Description

The mediaId FK on media_request rows was being set to NULL shortly after insert. This was caused by mediaRepository.save(media) being called with the requests relation loaded, which triggered TypeORM's cascade ['insert', 'update'] reconciliation on all loaded request entities, corrupting the FK. The issue was compounded by these saves not being awaited in updateParentStatus, creating race conditions between concurrent subscriber methods (updateParentStatus, sendToSonarr, sendToRadarr) all firing from afterInsert.

Additionally, previously, declining a TV request did not reset the associated season statuses back to UNKNOWN, preventing those seasons from being re-requested. The pending request count check also did not exclude the request being declined or scope to the correct is4k variant, causing the media status to remain stuck at PENDING.

Related to #2316

[info][Media Request]: Sent request to Sonarr {"requestId":2871,"mediaId":3802}
[info][Media Request]: Sent request to Sonarr {"requestId":2872,"mediaId":3803}
[info][Media Request]: Sent request to Sonarr {"requestId":2873,"mediaId":3804}

Yet the database showed NULL for the mediaId column on those same records:

  id  | mediaId |           createdAt           
------+---------+-------------------------------
 2871 |         | 2026-01-28 04:35:16.162949+00
 2872 |         | 2026-01-28 04:37:23.039107+00
 2873 |         | 2026-01-28 04:45:12.229032+00

How Has This Been Tested?

  • Auto-approved request (movie & TV): mediaId not null
  • Pending request → manual approve: sent to Radarr/Sonarr, notifications fire
  • Decline movie request: media status resets to UNKNOWN
  • Decline TV request (last pending): media status resets to UNKNOWN
  • Decline TV request (other pending exists): media status stays PENDING
  • Decline TV request: season statuses reset, can re-request
  • Delete a request: media status resets appropriately

Screenshots / Logs (if applicable)

Checklist:

  • I have read and followed the contribution guidelines.
  • Disclosed any use of AI (see our policy)
  • I have updated the documentation accordingly.
  • All new and existing tests passed.
  • Successful build pnpm build
  • Translation keys pnpm i18n:extract
  • Database migration (if required)

@fallenbagel fallenbagel requested a review from a team as a code owner January 29, 2026 16:58
@gauthier-th
Copy link
Member

Related to #2316

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an intermittent TypeORM relation-mapping issue that could create media_request rows with a NULL mediaId, leading to orphaned requests and frontend crashes when request.media is missing.

Changes:

  • Adds an explicit @JoinColumn({ name: 'mediaId' }) to the MediaRequest.media relation.
  • Removes the separate mediaId column/property and stops manually setting mediaId during request creation, relying on the relation mapping instead.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@fallenbagel fallenbagel changed the title fix: add explicit JoinClumn to MediaRequest media relation fix: try to fix MediaRequest mediaId null issue Feb 4, 2026
@github-actions github-actions bot added the merge conflict Cannot merge due to merge conflicts label Feb 4, 2026
@github-actions
Copy link

github-actions bot commented Feb 4, 2026

This pull request has merge conflicts. Please resolve the conflicts so the PR can be successfully reviewed and merged.

@fallenbagel fallenbagel force-pushed the fallenbagel/fix/typeorm-mediaid-null-issue branch 2 times, most recently from 63b6433 to a74b313 Compare February 5, 2026 06:37
@github-actions github-actions bot removed the merge conflict Cannot merge due to merge conflicts label Feb 5, 2026
@fallenbagel fallenbagel force-pushed the fallenbagel/fix/typeorm-mediaid-null-issue branch from a74b313 to c5479c0 Compare February 5, 2026 06:42
@fallenbagel fallenbagel changed the title fix: try to fix MediaRequest mediaId null issue fix(media-request-subscriber): prevent mediald nullification from cascade saves Feb 6, 2026
@fallenbagel fallenbagel added the bug Something isn't working label Feb 7, 2026
@fallenbagel fallenbagel requested a review from Copilot February 7, 2026 01:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 7 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@fallenbagel fallenbagel force-pushed the fallenbagel/fix/typeorm-mediaid-null-issue branch from 03ad825 to d78a996 Compare February 7, 2026 04:06
@fallenbagel fallenbagel requested a review from Copilot February 8, 2026 06:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@fallenbagel fallenbagel force-pushed the fallenbagel/fix/typeorm-mediaid-null-issue branch from 7de1b36 to 902a69c Compare February 8, 2026 09:48
Fixes intermittent NULL mediaId foreign key on media_request records byadding explicit @joincolumn
decorator to the media relation. Without this,TypeORM's implicit FK mapping was unreliable, causing
orphaned requeststhat would crash the frontend when accessing user profiles. Also removes the
redundant @column decorator for mediaId which conflicted withthe relation, and removes explicit
mediaId assignments in the constructorwhich are now handled correctly by TypeORM through the
relation.
…cade saves

The mediaId FK on `media_request` rows was being set to NULL shortly after insert caused by
`mediaRepository.save(media)` being called with the requests relation loaded which in turn triggered
TypeORM's cascade reconciliation on all loaded request entities, corrupting the FK. This issue must
have even gotten worse when the saves were not being awaited in updateParentStatus which could have
caused race conditions between concurrent subscriber methods (updateParentStatus, sendToSonarr,
sendToRadarr) all firing from afterInsert.

fix #2315
@fallenbagel fallenbagel force-pushed the fallenbagel/fix/typeorm-mediaid-null-issue branch from 902a69c to d73f039 Compare February 9, 2026 11:33
gauthier-th
gauthier-th previously approved these changes Feb 9, 2026
M0NsTeRRR
M0NsTeRRR previously approved these changes Feb 13, 2026
@fallenbagel fallenbagel merged commit 1ed86c1 into develop Feb 13, 2026
10 checks passed
@fallenbagel fallenbagel deleted the fallenbagel/fix/typeorm-mediaid-null-issue branch February 13, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting media triggers erroneous 'Available' notifications for requests migrated from Overseerr [Bug] MediaRequest records created with NULL mediaId

3 participants