我在练习网站上学习django(1.9.2),在psycopg2学习postgresql数据库。
我定义了一个具有特定属性的模型"预览"然后完全删除了该属性。尽管删除了它,django似乎是从缓存或其他东西引用旧的定义。
makemigrations
命令似乎工作正常,反映了我对模型定义所做的每一个更改,但是一旦我运行migrate
命令,就会弹出这个错误。
(env) D:\Web Workspace\Rat Race Website\ratrace>python manage.py migrate
Operations to perform:
Apply all migrations: contenttypes, news, polls, auth, sessions, admin
Running migrations:
Applying news.0003_auto_20160212_1209...Traceback (most recent call last):
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: syntax error at or near "9999999999999"
LINE 1: ...ER TABLE "news_news" ADD COLUMN "preview" varchar(9999999999...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\base.py", line 399, in execute
output = self.handle(*args, **options)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\commands\migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_ini
tial)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\operations\fields.py", line 62, in database_forwards
field,
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\base\schema.py", line 396, in add_field
self.execute(sql, params)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\base\schema.py", line 110, in execute
cursor.execute(sql, params)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\utils.
py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\utils\six
.py", line 685, in reraise
raise value.with_traceback(tb)
File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "9999999999999"
LINE 1: ...ER TABLE "news_news" ADD COLUMN "preview" varchar(9999999999...
数据库表是空的,所以我怀疑删除数据库并创建一个新数据库可能暂时解决了这个问题,但我想要一个更具体的解决方案,以防将来再次发生删除数据库不是一种选择。
这是模型定义的样子。
from django.db import models
# Create your models here.
class News(models.Model):
headline = models.CharField(max_length=100)
content = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published')
content_preview = models.CharField(max_length=100, blank=True)
thumbnail = models.ImageField(upload_to='thumbnails/%Y/%m/%d/', blank=True)
def __str__(self):
return self.headline
没有&#34;预览&#34;属性了,但django似乎没有那个。
编辑: 这是预览字段的定义
preview = CharField(max_length=9999999999999)
它产生了一个错误,说它不能为空,所以我输入了一次默认值"news preview"
。我假设默认值是现有条目。当时大约有5个测试条目。
请阅读另一篇文章,我在输入一次性默认值时使用的引号是问题的可能原因。关于psycopg2不喜欢引用值的东西?...这是一个字符串值,所以我认为我需要引号。
无论如何,现在我已经清除了预览字段,如何让django忘记它曾经存在?
答案 0 :(得分:1)
取消其他评论和我自己的经验。 。 。当您收到此错误时,请进入迁移文件夹并删除__init__.py
以外的所有内容。您甚至可以删除__pycache__
文件夹。然后再次运行makemigrations
和migrate
。我认为应该解决这个问题。
答案 1 :(得分:0)
如果不指定max_length,则使CharField可为空时也会发生这种情况。
所以解决方案是在模型中指定max_length,然后删除并重新制作有问题的迁移。