Postgres Table not updating correctly on save with autocommit on
|Reported by:||anonymous||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.2|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Description (last modified by )
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.