Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding a user/accounts table to Postgres in Django View
    primarykey
    data
    text
    <p><strong>Go to edit 2</strong></p> <p>Calling the following adduser function in views.py. I save the user first because it's id (automatically created by Django upon INSERT) is the primary/foreign key for accounts and passwords. Adding a user seems to be working fine, but then when it gets to the <code>Accounts(user=u)</code>, the following error throws: </p> <pre><code>IntegrityError at /adduser insert or update on table "OmniCloud_App_accounts" violates foreign key constraint "user_id_refs_id_468fbcec324e93d2" DETAIL: Key (user_id)=(4) is not present in table "OmniCloud_App_user". </code></pre> <p>But the key should be there since it just saved the user to the db...</p> <pre><code>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/") </code></pre> <p>EDIT: Here is the beginning to the initialization of Accounts:</p> <pre><code>from django.db import models from django.contrib.auth.models import User class Accounts(models.Model): user = models.ForeignKey(User) </code></pre> <p><em><strong>EDIT 2</em></strong> I've realized, by going to the admin page, that when It complains that 4 isn't in the database, that is because it is adding the 3rd user. Adding the 4th user throws an error that there is no user with id 5 (duh). I don't see anything in the code that would cause it to search for user_id+1, any ideas</p> <p>To the war chest!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload