我在我的django应用程序上测试我的模型,每当我尝试保存我的扩展用户模型(使用OneToOne关系)时,它会抛出该错误
models.py
class UserData(models.Model):
lastSeason = models.IntegerField(default=0)
lastChapter = models.IntegerField(default=0)
user = models.OneToOneField(User,on_delete=models.CASCADE)
def __str__(self):
return self.user.username
这是我在manage.py shell上所做的:
>>> from django.contrib.auth.models import User
>>> from mediaSort.models import Show, UserData
>>> import datetime
>>> usL = User.objects.get(username='luciano')
>>> newUs = UserData(lastSeason=1,lastChapter=2,user=usL)
>>> newUs.save()
这是错误:
Traceback (most recent call last):
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: mediaSort_userdata.user_id
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 880, in _do_insert
using=using, raw=raw)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\compiler.py", line 1280, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Luciano\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: mediaSort_userdata.user_id
我在这里遗漏了什么吗?提前致谢!
答案 0 :(得分:0)
看起来你已经有了一个用户Luciano的UserData对象,请尝试检查是否属实。如果可以在db for User和UserData表中发布所有条目,将会很有帮助。