onkar
onkar

Reputation: 4547

IntegrityError - Column 'user_id' cannot be null

I am trying to register a new user on the site,

class UserInfo(models.Model):
    user = models.ForeignKey(User,primary_key=True)#user profile
    email_id=models.CharField(max_length=32, null=True, blank=True)   

When I am registering the user, I am getting stuck by Integrity Error, please help me to resolve the problem.

def registration(request):
    registration_dict = {}
    if 1==1 :
    #if request.POST:
        #username=request.POST['email']
        #password=request.POST['password']
        username="[email protected]"
        password='123456'
        #try:
        UserInfo.objects.get_or_create(email_id=username,user__username=username,user__email=username,user__password=password)
        #except:
          #  registration_dict["status"]="0"
         #   registration_dict["message"]="Username already present"
           # return HttpResponse(simplejson.dumps(registration_dict),content_type="application/json")  

        registration_dict["status"]="1"
        registration_dict["message"]="Thank You for registering"
        return HttpResponse(simplejson.dumps(registration_dict),content_type="application/json") 
    else:
        registration_dict["status"]="0"
        registration_dict["message"]="Unable to process the request"
        return HttpResponse(simplejson.dumps(registration_dict),content_type="application/json")    

EDIT 1 I have tried changing UserInfo.objects.get_or_create(email_id=username,user__username=username,user__email=username,user__password=password,user_id=1) and then the error changes, to

'Cannot add or update a child row: a foreign key constraint fails (`app_info`.`appdata_userinfo`, CONSTRAINT `user_id_refs_id_b0fd803b` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')

Upvotes: 0

Views: 2534

Answers (3)

mtnpaul
mtnpaul

Reputation: 704

From the limited information I would say the problem is it does not find a UserInfo that matches. It then tries to create a new UserInfo, but it has no User to assign to the User ForeignKey. I would suggest the following:

user = authenticate(username=email, password=password)
if user is None:
     user = User(username=email, password=password, email=email)
user_info = UserInfo.objects.get_or_create(user=user, email_id=email)

Upvotes: 1

anupraj
anupraj

Reputation: 7

I cant understand why you need UserInfo because email is already there in User. Issue can be corrected by splitting the fetching process

username = "[email protected]"
password = '123456'

user,status = User.objects.get_or_create(username=username, password=password)
user_info = UserInfo.objects.get_or_create(user=user,email_id=username)

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174614

If the original User object doesn't exist, you'll run into all kinds of problems. So, you need to break the process down into two steps.

  1. Check if a User object exists or not, if it doesn't create it.
  2. Check if a UserInfo object exists for that user, if it doesn't create it.

As there is a ForeignKey, you cannot do it in one step:

username = "[email protected]"
password = '123456'

obj, created = User.objects.get_or_create(username=username)
obj.set_password(password) # the proper way to set the password
obj.save()

# Now fetch or create a UserInfo object
info, created = UserInfo.objects.get_or_create(email_id=username,user=obj)

Upvotes: 1

Related Questions