3

I am getting an error that is quite weird and I have no clue what could be causing that. I just want to do a simple signup using either Facebook or on-site registration using AllAuth (a third party registration app).

Somehow every time I try to register a new user a new ID is created and a new USER_ID as well. But they are not the same (superuser Id = 1 apart), one seems to be always even and the other one odd. It's like two new users are being created at the same time, but one lacks the USER_ID bringing this integrity error. I even tried to DROP my original database to see if it helps, but the issue remained.

I'm using Django 1.8 and Python 3.5

It was working fine until I decided to do a little extension to the user module as shown bellow:

profiles/models.py (updated with a few prints in post_save_user_model_receiver and sometimes it does access this function twice in a row which is really weird, since every time I get the integrity error)

from django.conf import settings
# from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.utils.encoding import smart_text

UF_CHOICES = ( ('SP', 'SP'), ('RJ', 'RJ'), )

User = settings.AUTH_USER_MODEL # 'auth.User'

class Profile(models.Model):
    user = models.OneToOneField(User)
    id = models.AutoField(primary_key=True)

    uf = models.CharField(max_length = 2, blank=False, default = 'SP', choices = UF_CHOICES, verbose_name = 'UF',)
    cidade = models.CharField(max_length=120, null=True, blank=True, default="Berlin")
    telefone = models.CharField(max_length = 20, blank=True, null=True, verbose_name = 'Telefone para Contato', default=1000)


    def __str__(self):
        return smart_text(self.user.username)

def post_save_user_model_receiver(sender, instance, created, *args, **kwargs):
    print ('post_save_user_model_receiver')
    if user.username:
        print('created')
        if created:
            print('try')
            try:
                Profile.objects.create(user=instance)
            except:
                pass
    else:
        print('not created')


post_save.connect(post_save_user_model_receiver, sender=User)

But now I am getting this IntegrityError from a "user_id" that I didn't specify anywhere (guessing it has to do with AllAuth, but not sure).

I did a few tests and it is really strange:

In psql in the Terminal I can see this:

You are now connected to database "hunters" as user "Alex".
hunters=# TABLE profiles_profile;
 id | uf | cidade | telefone | user_id 
----+----+--------+----------+---------
  1 | SP | Berlin | e33      |       1
  2 | SP | Berlin |          |       2
  4 | SP | Berlin |          |       3
(3 rows)

In the Django Shell:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from profiles.models import Profile
>>> Profile.objects.all()
[<Profile: alexcampos>, <Profile: alex>, <Profile: alx>]
>>> Profile.objects.get(id=1)
<Profile: alexcampos>
>>> Profile.objects.get(id=2)
<Profile: alex>
>>> Profile.objects.get(id=4)
<Profile: alx>
>>> Profile.objects.get(id=3)
Traceback (most recent call last):
  File "/Users/Alex/Desktop/Hunters/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 69, in handle
    self.run_shell(shell=options['interface'])
  File "/Users/Alex/Desktop/Hunters/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 61, in run_shell
    raise ImportError
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/Alex/Desktop/Hunters/lib/python3.5/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/Alex/Desktop/Hunters/lib/python3.5/site-packages/django/db/models/query.py", line 334, in get
    self.model._meta.object_name
profiles.models.DoesNotExist: Profile matching query does not exist.
>>> 

Above I tried to get all the objects by Id to find out that id=3, id=5, and so on, do not exist. Only even numbers and the superuser were found.

and the error message:

IntegrityError at /accounts/signup/
null value in column "user_id" violates not-null constraint

DETAIL:  Failing row contains (5, SP, Berlin, 3332211, null).

Above, 5 is the ID and null is the USER_ID.

If anybody has any idea of how to fix it or just what could be causing that I would really appreciate.

Thanks!!

A Campos
  • 753
  • 3
  • 10
  • 31
  • 2
    In the end I could not find the error. I guess it was some migrations that went wrong as I did some forbidden operations. What I end up doing was restarting a whole new django virtual environment and migrating the old project to the new one. As I did that things started to work perfectly. – A Campos Mar 13 '17 at 17:31

0 Answers0