Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#17318 closed Bug (invalid)

'Topic authentication', section 'Storing additional information about users' there is bug in the code

Reported by: supreet.sethi@… Owned by: nobody
Component: Documentation Version: 1.3
Severity: Normal Keywords: UserProfile
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In the example code for creating profile using post_save event of User. The code is written thus

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

Whereas it should be thus:

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance).save()

Otherwise it generates "IntegrityError: column user_id is not unique" error.

Change History (2)

comment:1 by Karen Tracey, 13 years ago

Resolution: invalid
Status: newclosed

The proposed change is unnecessary (create() already saves the new object to the DB) and would not do anything to fix an IntegrityError. You'd get an IntegrityError if somehow this create_user_profile was called with create set to True for some user instance where there was already a UserProfile object for it in the DB. I'm not sure how that would have happened, but tacking .save() onto that existing line of code wouldn't fix that problem.

comment:2 by bwanamarko@…, 12 years ago

as stated in the Status comment by @kmtracy m it raises the integrity exception because the UserProfile is already created changing the create to get_or_create solves the problem.

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.get_or_create(user=instance)
Note: See TracTickets for help on using tickets.
Back to Top