你好django框架,我试图从2天开始解决这个问题 我在python manage.py migrate命令后收到此错误。 我不明白问题是什么?
models.py
from django.db import models
from django.utils import timezone
class BlogPost(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=100,unique=True)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True,null=True)
def publish(self):
self.published_date=timezone.now()
self.save()
def __str__(self):
return self.title
错误
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages /django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 515, in alter_field
old_db_params, new_db_params, strict)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 262, in _alter_field
self._remake_table(model, alter_field=(old_field, new_field))
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 206, in _remake_table
self.quote_name(model._meta.db_table),
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/letsperf/.conda/envs/MyDjangoEnv/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: social_blogpost.title
请帮忙我很困惑!!
答案 0 :(得分:1)
根据Django documentation,这是问题所在的摘要;
应用“普通”迁移,将唯一的非空字段添加到 具有现有行的表将引发错误,因为使用的值 填充现有行仅生成一次,因此破坏了 唯一约束。