Changes between Initial Version and Version 1 of Ticket #17241


Ignore:
Timestamp:
Nov 16, 2011, 6:56:04 PM (13 years ago)
Author:
Karen Tracey
Comment:

Fixed formatting. Please use WikiFormatting and preview before posting.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #17241 – Description

    initial v1  
    11In a model for my database I am trying to define an Accounts table with the id of a default Django User.
    2 
     2{{{
     3#!python
    34class Accounts(models.Model):
    4 
    5 user = models.OneToOneField(User)
     5    user = models.OneToOneField(User)
     6}}}
    67When I have it set up like this and try to add a user in the adduser view:
    78
     9{{{
     10#!python
    811def adduser(request):
    9 username = request.POST['username']
    10 password = request.POST['password']
    11 u = User.objects.create_user(username, request.POST['email'], password)
    12 u.save()
    13 a = Accounts(user=u)
    14 p = Passwords(user=u)
    15 a.save()
    16 p.save()
    17 user = authenticate(username=username, password=password)
    18 if user is not None and user.is_active:
    19     auth.login(request, user)
    20     return HttpResponseRedirect("/%s/" %u.id)
    21 else:
    22     return HttpResponseRedirect("/account/invalid/")
     12    username = request.POST['username']
     13    password = request.POST['password']
     14    u = User.objects.create_user(username, request.POST['email'], password)
     15    u.save()
     16    a = Accounts(user=u)
     17    p = Passwords(user=u)
     18    a.save()
     19    p.save()
     20    user = authenticate(username=username, password=password)
     21    if user is not None and user.is_active:
     22        auth.login(request, user)
     23        return HttpResponseRedirect("/%s/" %u.id)
     24    else:
     25        return HttpResponseRedirect("/account/invalid/")
     26}}}
     27
    2328There is an integrity error:
    24 
     29{{{
    2530IntegrityError at /adduser
    2631insert or update on table "XXXX_accounts" violates foreign key constraint   "user_id_refs_id_468fbcec324e93d2"
    2732DETAIL:  Key (user_id)=(10) is not present in table "XXXX_user".
     33}}}
    2834adducer 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.
Back to Top