我正在學習django(1.9.2)在練習網站和postgresql數據庫與psycopg2。 我用某個屬性「預覽」定義了一個模型,後來完全刪除了該屬性。儘管刪除了它,django似乎是從緩存或其他東西引用舊的定義。 makemigrations
命令似乎工作正常,反映了我對模型定義所做的每一個更改,但是一旦我運行migrate
命令,則會彈出此錯誤。Django遷移ProgrammingError:語法錯誤在「」
(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
沒有「預覽」屬性了,但Django似乎沒有得到。
編輯: 下面是預覽場
preview = CharField(max_length=9999999999999)
定義它產生一個錯誤,說這不可能是空的,所以我進入了一個時間默認值爲"news preview"
。我假設默認值是針對現有條目的。當時有大約5個測試條目。 只需閱讀另一篇文章,即在輸入一次默認值時使用的引號是導致問題的可能原因。關於psycopg2不喜歡引用值的東西?...這是一個字符串值,所以我認爲我需要引號。
不管怎樣,現在我已經清除了預覽字段,我該如何讓django忘記它曾經存在?
嘗試添加列時似乎失敗,這可能是您之前沒有運行的遷移。預覽字段的定義是什麼? – cdvv7788
剛剛添加到這個問題。這有幫助嗎? –
您需要修改您的遷移(編輯它們或刪除/重新創建)。不過,我認爲場地的長度可能是原因。我可以檢查一下。 – cdvv7788