fix(mediarequest): explicitly set mediaId when creating request (#2316)

* fix(mediarequest): explicitly set mediaId when creating

Intermittent issue where media_request records were created with mediaId = NULL,causing TypeError
when accessing request.media.tmdbId on the profile page. TypeORM's implicit relation-to-foreign-key
mapping was failing intermittently. This sets the mediaId column explicitly and adds a guard to
check to fail fast if media.id is not populated after save.

fix #2315

* refactor: better logging when media id not found
This commit is contained in:
fallenbagel
2026-01-23 14:32:46 +05:00
committed by GitHub
parent 88b2e7843f
commit beba2ea099

View File

@@ -332,9 +332,16 @@ export class MediaRequest {
if (requestBody.mediaType === MediaType.MOVIE) {
await mediaRepository.save(media);
if (!media.id) {
throw new Error(
`Failed to save media before creating request. Media type: ${requestBody.mediaType}, TMDB ID: ${requestBody.mediaId}, persisted media id: ${media.id}`
);
}
const request = new MediaRequest({
type: MediaType.MOVIE,
media,
mediaId: media.id,
requestedBy: requestUser,
// If the user is an admin or has the "auto approve" permission, automatically approve the request
status: user.hasPermission(
@@ -442,9 +449,16 @@ export class MediaRequest {
await mediaRepository.save(media);
if (!media.id) {
throw new Error(
`Failed to save media before creating request. Media type: TV, TMDB ID: ${requestBody.mediaId}, is4k: ${requestBody.is4k}`
);
}
const request = new MediaRequest({
type: MediaType.TV,
media,
mediaId: media.id,
requestedBy: requestUser,
// If the user is an admin or has the "auto approve" permission, automatically approve the request
status: user.hasPermission(
@@ -521,6 +535,9 @@ export class MediaRequest {
})
public media: Media;
@Column({ name: 'mediaId', nullable: true })
public mediaId: number;
@ManyToOne(() => User, (user) => user.requests, {
eager: true,
onDelete: 'CASCADE',