Lochan SS
Lochan SS

Reputation: 129

IntegrityError at / NOT NULL constraint failed: todolist_todolist.ischecked

i wonder what is causing this error, deleted migration folder then again performed makemigrations todolist and migrate . Kept null=True , all these efforts in vain

models.py

class Category(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        verbose_name = ("Category")
        verbose_name_plural = ("Categories")
    def __str__(self):
        return self.name

class ToDoList(models.Model):
    title = models.CharField(max_length=200)
    content = models.CharField(max_length=500)
    created_on = models.DateField(default=timezone.now().strftime("%Y-%m-%d"))
    due_date = models.DateField(default=timezone.now().strftime("%Y-%m-%d"))
    category = models.ForeignKey(Category,on_delete=models.DO_NOTHING,default="general",null=True, blank=True) //kept blank and null field true


    class Meta:
      ordering = ["-created_on"]
    def __str__(self):
        return self.title

views.py

def index(request):
    ToDos = ToDoList.objects.all()
    catogories = Category.objects.all()

    if request.method == 'POST':
        if "taskAdd" in request.POST:
            title = request.POST["description"]
            category = request.POST["category_select"]
            date = str(request.POST["date"])
            content = title + "--" + date + "--" + category


            ToDo = ToDoList(
                title = title,
                content = content,
                due_date = date,
                category = Category.objects.get(name=category),
                )
            ToDo.save()
            return redirect("/")

        if "taskDelete" in request.POST:

            checkboxlist = request.POST["checkedbox"]

            for todo_id in checkboxlist:
               todo = ToDoList.objects.get(id=int(todo_id))
            todo.delete()

    return render(request,"index.html",{"ToDos" : ToDos,"catogories" : catogories})

0001_initaly.py //migration code

  migrations.CreateModel(
            name='ToDoList',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=200)),
                ('content', models.CharField(max_length=500)),
                ('created_on', models.DateField(default='2020-03-29')),
                ('due_date', models.DateField(default='2020-03-29')),
                ('category', models.ForeignKey(blank=True, default='general', null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='todolist.Category')),
            ],
            options={
                'ordering': ['-created_on'],
            },
        ),

error pic enter image description here

admin/todolist pic //which doesn't contain ischecked field enter image description here

thanks in advance

Upvotes: 0

Views: 552

Answers (1)

jTiKey
jTiKey

Reputation: 743

Before deleting a migration folder, you have to migrate that app to zero (it will remove all the tables for that app):

python manage.py migrate yourapp zero

Because your database is still in the state of your deleted migration files. Just deleting the files won't change your database structure. Try reverting your removal, migrating, then deleting the migration folder. If you can't recover your folder, then you have clear that table manually.

Upvotes: 1

Related Questions