Opened 13 years ago

Last modified 13 years ago

#17241 closed Bug

Postgres Table not updating correctly on save with autocommit on — at Version 1

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Karen Tracey)

In a model for my database I am trying to define an Accounts table with the id of a default Django User.

class Accounts(models.Model):
    user = models.OneToOneField(User)

When I have it set up like this and try to add a user in the adduser view:

def adduser(request):
    username = request.POST['username']
    password = request.POST['password']
    u = User.objects.create_user(username, request.POST['email'], password) 
    u.save()
    a = Accounts(user=u)
    p = Passwords(user=u)
    a.save()
    p.save()
    user = authenticate(username=username, password=password)
    if user is not None and user.is_active:
        auth.login(request, user)
        return HttpResponseRedirect("/%s/" %u.id)
    else:
        return HttpResponseRedirect("/account/invalid/")

There is an integrity error:

IntegrityError at /adduser
insert or update on table "XXXX_accounts" violates foreign key constraint   "user_id_refs_id_468fbcec324e93d2"
DETAIL:  Key (user_id)=(10) is not present in table "XXXX_user".

adducer should be saving the user before creating the Accounts class to corresponds, so it shouldn't have an issue with the id being generated automatically by Django, but apparently Postgres has an issue with ForeignKeys.

Change History (1)

comment:1 by Karen Tracey, 13 years ago

Description: modified (diff)

Fixed formatting. Please use WikiFormatting and preview before posting.

Note: See TracTickets for help on using tickets.
Back to Top