Skip to content

Piccolo doesn't handle duplicate entries automatically for in .add_m2m  #707

@Drapersniper

Description

@Drapersniper

Discussed in #683

Originally posted by Drapersniper November 21, 2022

class Track(Table):
    track_id = BigInt(primary_key=True)

class Playlist(Table):
    id = BigInt(primary_key=True)
    tracks = M2M(LazyTableReference("TrackToPlaylist", module_path=__module__))

class Album(Table):
    id = BigInt(primary_key=True)
    tracks = M2M(LazyTableReference("TrackToAlbum", module_path=__module__))

class TrackToPlaylist(Table):
    tracks = ForeignKey(Track)
    playlists= ForeignKey(Playlist)    

class TrackToAlbum(Table):
    tracks = ForeignKey(Track)
    albums= ForeignKey(Album)

# Step 1 -  I expect this to work fine if this is the first time the tracks with id 0-9 are being added to the db
playlist = await Playlist.objects().get_or_create(Playlist.id == 1)
tracks = [Track(track_id=i) for i in range(10)]
await playlist.add_m2m(*tracks, m2m=Playlist.tracks)

# Step 2 - After step 1 do the following
playlist2 = await Playlist.objects().get_or_create(Playlist.id == 2)
tracks2 = [Track(track_id=i) for i in range(5)]
await playlist2 .add_m2m(*tracks2, m2m=Playlist.tracks)

# Step 3
playlist3 = await Playlist.objects().get_or_create(Playlist.id == 3)
tracks3 = [Track(track_id=i) for i in range(5)]
tracks3 +=  [Track(track_id=i) for i in range(5)]
await playlist3 .add_m2m(*tracks3, m2m=Playlist.tracks)

# Step 4 - Run after step 1
album = await Album.objects().get_or_create(Album.id == 1)
tracks4 = [Track(track_id=i) for i in range(5)]
await album .add_m2m(*tracks4, m2m=Album.tracks)

What is expected to happen after steps 2, 3, and 4?

Currently it raises an exception

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions