Exception Value: column user_id is not unique

52 views
Skip to first unread message

HannesP

unread,
Nov 19, 2013, 4:34:35 PM11/19/13
to django...@googlegroups.com

I'm Django noob so please bear with me. I am trying to make a simple gallery for usres so that they can upload pics to their profile. Here are the relevant parts:

models.py

def get_uplaod_file_name(instance,filename):
    return 'uploaded_files/%s_%s' % (str(time()).replace('.','_'), filename)

class UserPic(models.Model):
    user = models.ForeignKey(User, unique=False)
    picfile = models.FileField(upload_to=get_uplaod_file_name)

    @models.permalink
    def get_absolute_url(self):
        return ('view_pirate', None, {'user': self.account.user})    

    def __unicode__(self):
        return unicode(self.picfile.name) 

views.py

@login_required
def list(request):
    # Handle file upload
    if request.method == 'POST':
        picform = PicForm(request.POST, request.FILES)
        if picform.is_valid():

            newpic = UserPic(picfile = request.FILES['picfile'])
            newpic = picform.save(commit=False)
            newpic.user = request.user
            newpic.save()
            message = "file %s is uploaded" % newpic
            userpics = UserPic.objects.all()

            return render_to_response('userpics/listpics.html',
                                      {'userpics': userpics, 'picform': picform},
                                      context_instance=RequestContext(request)
    )


    else:
        picform = PicForm() # A empty, unbound form


    userpics = UserPic.objects.all()


    return render_to_response(
        'userpics/listpics.html',
        {'userpics': userpics, 'picform': picform},
        context_instance=RequestContext(request)
    )

The app works fine for one image upload but the problem is that whenever a user tries to upload a second image, Django gives this error:

column user_id is not unique

I have also tried 'unique= True on a fresh database in the model, but still got stocked at this problem. Appreciate your hints.

Thanks

tim

unread,
Nov 19, 2013, 7:36:14 PM11/19/13
to django...@googlegroups.com
If you first run syncdb with the UserPic.user field unique=True, it's not enough just to change unique=False. You'll also need to remove the database unique constraint. Do you have the problem on a fresh syncdb if unique=False? If so, it would be helpful to include the full traceback of the error.

HannesP

unread,
Nov 19, 2013, 7:59:58 PM11/19/13
to django...@googlegroups.com
Thanks for tip tim.
Actually I have noticed a weird thing. Hope you can explain why it happened:
Initially, I had installed south. when I created a fresh sqlite database, the module that I created always came separate form others. That is .manage.py syncdb resulted in


Not synced (use migrations):
 - userpics

Then after migrate the database was created.

Then I disabled south module and started with a fresh database (with unique=False in the model) and I saw taht the error is gone. Now I can upload multiple images. How is it so? What could be wrong with the module that south treated it separately?

Thanks,
Hannes

tim

unread,
Nov 19, 2013, 8:14:21 PM11/19/13
to django...@googlegroups.com
Did you have unique=True when you created the database with south? If not, I cannot any reason for the discrepancy you are describing. south does treat apps that have migrations separately from the rest, but that doesn't seem to be related to the issue you had.
Reply all
Reply to author
Forward
0 new messages