[Fixed]-UNIQUE constraint failed: music_playlist.owner_id

1👍

Please correct your code

class Playlist(models.Model):
    name = models.CharField(max_length=200, null=False, blank=False,default='')
    owner = models.ForeignKey(User, null=True)

    def __str__(self):
        return self.name
    @property
    def playlist_id(self):
        return self.id

When you make the foreign key owner unique, it means that one user can have only one playlist. When you try to add another playlist for the same user your get this error

UNIQUE constraint failed: music_playlist.owner_id

If you want one user to have only one playlist, use OneToOneField

Leave a comment