2

I've decided to drop a row from a field from a database i'm setting up in django. I've deleted it in models/form and completely re-ran the database (makemigrations, migrate). However, no matter what I do i keep getting an integrity error (NOT NULL constraint failed: index_user.email). I'm not sure why i'm getting this, as the field doesn't even exist anymore and I cant find any trace of it in any files. Anyone know how to solve this error?

models:

from django.db import models

# Create your models here.

class user(models.Model):
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=15)

    def __str__(self):
        return self.username + ' - ' + self.password

views:

def login(request):
    form = LoginForm(request.POST)
    if form.is_valid():
         username = form.cleaned_data["username"]
         password = form.cleaned_data["password"]
         user = authenticate(username=username, password=password)

         if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('loggedin.html')
            else:
                return HttpResponse("Account deleted or disabled")
         else:
            return HttpResponseRedirect('/invalid')

    return render(request, "login_page.html", {'form': form})

form:

class LoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

def clean(self, *args, **kwargs):
    username = self.cleaned_data.get("username")
    password = self.cleaned_data.get("password")
    if username and password:
        user = authenticate(username=username, password=password)
        if not user:
            raise forms.ValidationError("User does not exist.")
        if not user.is_active:
            raise forms.ValidationError("User is no longer active.")
    return super(UserLoginForm, self).clean(*args, **kwargs)
Abjilla22
  • 11
  • 4
  • 14
  • It's next to impossible to know what's wrong without you showing us what your models/form look like, how you changed them and how your migrations look like. – Khoi Sep 01 '16 at 15:23
  • Did you go about modifying the user that came prepackaged with the admin part of the site? – Michael Platt Sep 01 '16 at 15:33
  • I've updated the post with models/views/form. Problem I'm getting is when im trying to add a user to the database through the admin function, and keep getting the intergrity error. – Abjilla22 Sep 01 '16 at 15:51
  • When I am developing **and not in production**, the best way I deal with such errors is a [hack](http://stackoverflow.com/a/27408592/2996101). I delete the database and rebuild it. – raratiru Sep 01 '16 at 15:57

3 Answers3

1

An easy fix that I used when accidentally setting up my ForeignKey backward was using the vs code file tracking and discarding all changes. Before doing this you should check when your last commit was. I made a commit right before creating my new model so I only lost about 5 minutes of work.

  • You should how to use vscode file tracking so, everyone sees this post can understand directly how to do that. By the way, this is a good answer just make it more enlightening. – Willy satrio nugroho Sep 03 '20 at 06:00
0

If you are using Mysql as your database backend, then when you delete any field in the django model, even you run makemigrate and migrate, Mysql will still NOT actually delete that field or column inside the database.

Therefore the column index_user.email, you think have deleted, should still inside the database, that gives you the error.

You have to get into the mysql console or any mysql cilent to drop that column by yourself, not by django migration.

If you are using sqlite, unfortunately, you can't drop a column on a table. you can check the reason on the link below:

https://stackoverflow.com/a/8442173/4151886

and solution you can find on the link below:

https://stackoverflow.com/a/5987838/4151886

Community
  • 1
  • 1
bluebird_lboro
  • 601
  • 5
  • 17
  • I'm using sqlite3 (db built into django) any idea how I would go about dropping the column? Thanks for the reply! – Abjilla22 Sep 01 '16 at 15:45
0

you can run a fake migrations. It will let you by pass the error for now. Also try to delete your migrations first, if that does not work see if the below works.

python manage.py makemigrations python manage.py migrate --fake