I'm not getting NOT NULL CONSTRAINT FAILED error

I am using django 1.9.8. I know it’s no longer supported but I need this help. I have set null=False in my model fields. I know it’s set to False by default. But when I am using modelname.objects.create() in my view and I’m not passing the not full fields while using this create method. Even then also the data is getting saved in the database and I’m not getting any error. I don’t know why is this happening! Please Help!

1 Like

Hey there!
When posting questions, the more details you provide, the best the answer can be given.

From the information i have, i think you are missing a migration.
Did you ran the makemigrations and migrate management command?

Yep! I did. I think I got the problem. Is it like if we don’t pass the charfield fields as an argument to modelname.objects.create then an empty string (“”) is added by default?

Can you post the models and code that you are having problems?

class Csv_file(models.Model):
    last_edit_name = models.CharField(max_length = 250, null=False, blank=False)
    file_path = models.FileField(upload_to='Files', verbose_name='Files')
    filename = models.CharField(max_length=250, null=False)
    operation = models.IntegerField(null=False)

    def __str__(self):
        return self.filename

This is the model

obj = Csv_file.objects.create(file_path=csv_file, operation=option)

I am not getting error even though I am not passing last_edit_username and filename

Maybe try the other way around.

obj = Csv_file(file_path=csv_file, operation=option)
obj.save()

Are you using a custom manager or have you overridden the save method on your model? (Is there more to this model than what you have posted here?)

No there is nothing more with this model. I have posted the whole model code over here!

I have tried that as well and that too is not throwing any sort of error

What database engine are you using? PostgreSQL? Sqlite? Something else? Also, what version of Python are you using with it?

sqlite and python 3.5.4

First the bottom line - given how obsolete this is, I don’t think you’re going to find too many people interested in trying to fix this. My first recommendation is to just accept that this is the behavior and move on.

I’d also examine the database to verify what’s actually getting stored in the table. If those columns are null, then my guess is that the version of sqlite you’re using isn’t enforcing that constraint.

The thing that is getting stored in database is empty string. I don’t know why?

If you really want to isolate the issue, then the first thing I’d try would be to try using it with PostgreSQL. I know that Sqlite can be very “flexible” in what it stores in columns, and it wouldn’t surprise me that that version of sqlite allows for it.