Skip to content

[Bug]: M3U Import fails due to Unique Constraint on duplicate tracks in tracks_to_playlists #485

Description

@jimbodetritus

Description
The M3U import process fails with a "Something went wrong" error when the playlist file contains duplicate tracks. While
the preview works correctly, the database transaction fails during the save operation.

Root Cause Analysis
The issue lies in mobile/src/data/playlist/api.ts. When createPlaylist or updatePlaylist is called, it attempts to insert
rows into the tracks_to_playlists table. If the M3U contains the same track multiple times, the Drizzle ORM transaction
hits a SQLite constraint violation because the table expects a unique combination of playlistName and trackId (or similar
primary key constraints defined in the schema).

Suggested Fix
Adding .onConflictDoNothing() to the insert operations in the playlist API prevents the transaction from failing when
duplicate tracks are encountered.

In mobile/src/data/playlist/api.ts:

1 // For createPlaylist
2 await tx.insert(tracksToPlaylists).values(
3   entry.tracks.map((t, position) => {
4     return { playlistName: name, trackId: t.id, position };
5   }),
6 ).onConflictDoNothing(); // <--- Add this
7
8 // And similarly for updatePlaylist
9 await tx.insert(tracksToPlaylists).values(

10 tracks.map((t, position) => {
11 const latestName = sanitizedName ?? id;
12 return { playlistName: latestName, trackId: t.id, position };
13 }),
14 ).onConflictDoNothing(); // <--- Add this

Steps to Reproduce

  1. Create an M3U file where the same file path is listed twice.
  2. Import the M3U in the app.
  3. Observe the success in preview but failure upon clicking "Save".

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIssue was opened via the bug report template.Solved / ImplementedClosed with a working solution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions