mdbhal
mdbhal

Reputation: 99

Django Integrity Error : Not Null constraint failed

I am trying to make a blog like website.

Here is my models.py:

class upload(models.Model):
    username = models.CharField(max_length=100)
    user = models.ForeignKey(settings.AUTH_USER_MODEL,default=1)
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=300,default=None)
    content = models.TextField(default=None,blank=True,null=True)
    docFile = models.FileField(default=None,blank=True,null=True)
    timestamp = models.DateTimeField(auto_now=False,auto_now_add=True)

    def __unicode__(self):
           return self.title

    class Meta:
           ordering = ["-timestamp"]

    def get_absolute_url(self):
           return reverse("detail",kwargs={"id":self.id})

I have changed my models.py to include the TextField(content) which was not present before

.Then I ran the command python manage.py makemigrations which executed successfully.After that when I run python manage.py migrate,I am getting

IntegrityError:NOT NULL constraint failed:uploads_upload.content

(uploads is the name of my app)

Reading some previous SO answers to this error,I have also added null=True and blank=True also to my content attribute,still I get the same error.

I am inputting the details through Django forms.Here is my forms.py:

class PostForm(forms.ModelForm):
#docfile = upload.docFile(required=False)
class Meta:
    model = upload
    fields = [
    "title",
    "description",
    "username",
    "content",
    "docFile",
    ]

Why am I getting this error?

Upvotes: 2

Views: 5977

Answers (1)

MrE
MrE

Reputation: 20806

When you changed your model to add the null=True and blank=True, you did not recreate the migration.

You need to re-run python manage.py makemigrations and you'll have a second migration to migrate

Upvotes: 3

Related Questions