Home python Not Null Constraint Failed, why?

Not Null Constraint Failed, why?

Author

Date

Category

When I try to create a superuser

python manage.py CreateSuperUser

I get the following error

django.db.utils.integrityerror: not null constraint failed: me_teacher.subject_id

ME / Models

from django.conf import settings
from django.contrib.auth.models Import User, AbstractUser
From Django.db Import Models
Import sys.
sys.path.append ('../')
From Wisdom.Models Import Subject
Class Teacher (AbstractUser):
  Name = Models.charfield (max_length = 20)
  Surname = Models.charfield (max_length = 20)
  User = Models.Onetoonefield (settings.auth_user_model,
    ON_DELETE = Models.Cascade)
  Subject = Models.ForeignKey (Subject, related_name = 'teacher_subject',
    ON_DELETE = Models.Cascade)

wisdom / models

from django.db import models
Class Subject (Models.Model):
  title = models.charfield (max_length = 200)
  Slug = Models.Slugfield (max_length = 200, Unique = True)
  Class Meta:
    Ordering = ['title']
  DEF __STR __ (SELF):
    Return Self.title

What is my problem?


Answer 1

We must provide the ability to create users without binding to the subject (NULL = TRUE )

subject = models.ForeignKey (subject, related_name = 'teacher_subject',
    NULL = TRUE, ON_DELETE = MODELS.CASCADE)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions