Django - 列" user_id"中的空值违反非空约束

时间:2017-05-11 22:12:18

标签: django python-3.x unit-testing

经过无数次试验后,我不断收到这个错误:

Traceback (most recent call last):
    File "C:\...\tests.py", line 82, in test_profile_creation
       w = self.create_profile()
    File "C:\...\tests.py", line 78, in create_profile
       self.user = Profile.objects.create(id=1)
    ...
    django.db.utils.IntegrityError: null value in column "user_id" violates not-null constraint
    DETAIL:  Failing row contains (1, null).

我试图覆盖这些区域:

enter image description here

我不确定如何使用标记为(get_screen_name() and get_association_name())的其他两种方法进行测试,因此指导会有所帮助。

这是我第一次参加单元测试并且仍然很新鲜,所以感谢你的指导,伙计们!

编辑2

tests.py

  def create_profile(self):
    self.asoc = Association.objects.create(id=2)
    self.admin = Administrator.objects.create(id=1, association=self.asoc)
    self.user = Profile.objects.create(id=2, user=self.admin)
    return Profile.objects.get(user=self.user, administrator=self.admin)

  Traceback (most recent call last):
    File "C:\...\tests.py", line 83, in test_profile_creation
        w = self.create_profile()
    File "C:\...\tests.py", line 79, in create_profile
        self.user = Profile.objects.create(id=2, user=self.admin)
    django.db.utils.IntegrityError: duplicate key value violates unique constraint "Profile_pkey"
     DETAIL:  Key (id)=(2) already exists.

编辑3

 def create_profile(self):
    self.asoc = Association.objects.create()
    self.admin = Administrator.objects.create(association=self.asoc)
    self.user = Profile.objects.create(user=self.admin)
    return Profile.objects.get(user=self.user, administrator=self.admin)


  Traceback (most recent call last):
     File "C:\..\tests.py", line 83, in test_profile_creation
        w = self.create_profile()
     File "C:\...\tests.py", line 79, in create_profile
        self.user = Profile.objects.create(user=self.admin)
     django.db.utils.IntegrityError: duplicate key value violates unique constraint "Profile_user_id_key"
     DETAIL:  Key (user_id)=(2) already exists.

编辑4

 def create_profile(self):
    self.asoc = Association.objects.create(id=7)
    self.admin = Administrator.objects.create(id=6, association=self.asoc)
    self.user = Profile.objects.create(id=5)
    return Profile.objects.get(user=self.user, administrator=self.admin)


  Traceback (most recent call last):
     File "C:\..\tests.py", line 83, in test_profile_creation
        w = self.create_profile()
     File "C:\...\tests.py", line 79, in create_profile
        self.user = Profile.objects.create(id=5)
     ...
     django.db.utils.IntegrityError: null value in column "user_id" violates not-null constraint
     DETAIL:  Failing row contains (5, null).

编辑5

 def create_profile(self):
    self.asoc = Association.objects.create(id=7)
    self.admin = Administrator.objects.create(id=6, association=self.asoc)
    self.user = Profile.objects.create(id=self.admin)        
    return Profile.objects.get(user=self.user, administrator=self.admin)


  Traceback (most recent call last):
     File "C:\..\tests.py", line 83, in test_profile_creation
        w = self.create_profile()
     File "C:\...\tests.py", line 79, in create_profile
        self.user = Profile.objects.create(id=self.admin)
     ...
     TypeError: int() argument must be a string, a bytes-like object or a number, not 'Administrator'

编辑6

 def create_profile(self):
    self.asoc = Association.objects.create(id=7)
    self.admin = Administrator.objects.create(id=6, association=self.asoc)
    self.user = Profile.objects.create(id=self.admin.id)        
    return Profile.objects.get(user=self.user, administrator=self.admin)


  Traceback (most recent call last):
     File "C:\..\tests.py", line 83, in test_profile_creation
        w = self.create_profile()
     File "C:\...\tests.py", line 79, in create_profile
        self.user = Profile.objects.create(id=self.admin.id)
     ...
     django.db.utils.IntegrityError: null value in column "user_id" violates not-null constraint
     DETAIL:  Failing row contains (6, null).

tests.py

class ProfileTest(TestCase):
    def create_profile(self):
        self.asoc = Association.objects.create(id=2)
        self.admin = Administrator.objects.create(id=1, association=self.asoc)
        self.user = Profile.objects.create(id=1)
        return Profile.objects.get(user=self.user, administrator=self.admin)

    def test_profile_creation(self):
        w = self.create_profile()
        self.assertTrue(isinstance(w, Profile))
        self.assertEqual(w.__str__(), w.user.username)

models.py

class Administrator(AbstractUser):
   ...
   association = models.ForeignKey(Association)


   class Meta:
       db_table = 'Administrator'

   def __str__(self):
      return self.username

   def __unicode__(self):
      return self.username



class Profile(models.Model):
   user = models.OneToOneField(Administrator)

   class Meta:
      db_table = 'Profile'

   def __str__(self):
       return self.user.username

   def get_screen_name(self):
       try:
           if self.user.get_full_name():
               return self.user.get_full_name()
           else:
               return self.user.username
       except:
           return self.user.username


    def get_association_name(self):
       try:
           if self.user.association:
               return self.user.association
           else:
               return self.user.username
       except:
           return self.user.username


 class Association(models.Model):
       asoc_name = models.CharField(max_length=50, null=True, blank=True, unique=True)

       class Meta:
          db_table = 'Association'

       def __str__(self):
          return self.asoc_name

       def __unicode__(self):
          return self.asoc_name

2 个答案:

答案 0 :(得分:2)

错误是因为您映射了OnetoOneField。并创建具有相同id的多个行,即1.尝试使用id创建2.它会起作用,但最好的方法是在添加id时将其传递为idauto incremented,您无需在创建时指定它,但如果要更新现有行,则可以指定它。希望它有所帮助

答案 1 :(得分:0)

个人资料通过OneToOneField与管理员相关联,因此在创建个人资料记录时,您需要设置user。所以在test.py -> line #78替换为以下内容:

self.user = Profile.objects.create(id=1, user=self.admin)