Search code examples
djangodatabasedatabase-designdjango-modelsmodels

django not nullable error on model field containing null-True


So i have a user that can post comments on effects, im linking up my models and i keep getting the non-nullable error no matter what ive tried. Everyone says it needs to have null=True. It isn't working lol. What am I not seeing here?

This is the official error:

django.db.utils.IntegrityError: NOT NULL constraint failed: effect_modules_comment__new.author_id

And my models:

class Effect_module(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=255)
    description = models.TextField()
    html = models.TextField(default='')
    css = models.TextField(default='')
    js = models.TextField(default='')
    up_votes = models.IntegerField()
    down_votes = models.IntegerField()
    effect_author = models.ManyToManyField('UserProfile')

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    effects = models.ManyToManyField(Effect_module)

class Comment(models.Model):
    comment_author = models.ForeignKey(User, null=True)
    comment = models.TextField(default='No Comment Here')
    effect_object = models.ForeignKey(Effect_module)

Solution

  • Delete all migration scripts. Add null=True means it can be NULL in the database, blank=True means that it can be left blank in forms.

    Then

    python manage.py makemigrations
    python manage.py migrate