2017-04-18 114 views
0

我在Heroku的django應用程序中創建了一個新的postgressql數據庫。但是,每次運行遷移時,都會收到一個我不明白的錯誤。Django和postgres遷移錯誤

錯誤:

Applying argent.0043_auto_20170322_1629...Traceback (most recent call last): 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) 
psycopg2.ProgrammingError: cannot cast type date to integer 
LINE 1: ...LTER COLUMN "date_id" TYPE integer USING "date_id"::integer,... 

我已刪除和添加唯一的日期字段我,也沒有用。無論我的模型中的日期字段是否存在,我仍然會得到相同的錯誤。任何有識之士將不勝感激。

models.py

class Entry(models.Model): 
    date = models.DateField(blank=True, null=True,) 
    euros = models.CharField(max_length=500, blank=True, null=True) 
    comments = models.CharField(max_length=900, blank=True, null=True) 
    euros_sum = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True) 
    xrate = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True) 
    dollars_sum = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True) 
    daily_savings_dollars = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True) 
    daily_savings_display = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True) 

    def get_absolute_url(self): 
     return reverse('argent:detail', kwargs={'pk': self.pk}) 

    def item_date(self): 
     row_title = self.date 
     return row_title 

    class Meta: 

     ordering = ['date'] 

回溯:

Traceback (most recent call last): 
    File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line 
    utility.execute() 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute 
    output = self.handle(*args, **options) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle 
    fake_initial=fake_initial, 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate 
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards 
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 215, in database_forwards 
    schema_editor.alter_field(from_model, from_field, to_field) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 513, in alter_field 
    old_db_params, new_db_params, strict) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field 
    new_db_params, strict, 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 674, in _alter_field 
    params, 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 119, in execute 
    cursor.execute(sql, params) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) 
django.db.utils.ProgrammingError: cannot cast type date to integer 
LINE 1: ...LTER COLUMN "date_id" TYPE integer USING "date_id"::integer,... 

回答

0

如果想從一個整數類型的日期列遷移到爲datetime一個,你需要處理的任何現有在您可以運行遷移之前,您在表中擁有的條目。 當您嘗試更改列類型時,您可能在date列中已經有一些整數,所以轉換失敗,因爲postgres不知道如何將整數轉換爲日期時間。

您可以刪除所有項目,如果你不關心他們,否則你需要打破你遷移到單獨的步驟:

  1. 創建一個臨時名稱新的datetime列
  2. 運行數據遷移以將整數轉換爲日期時間並填充新列
  3. 刪除舊的整數日期列,用最終名稱重命名新的列。
+0

這不是我要將日期列從整數轉換爲日期時間。它一直是一個日期時間列,並且新數據庫中沒有數據。我正在從sqlitedb轉換到posgresdb(對不起,我沒有提到過),只需要將我的models.py中的新字段寫入新的posgresdb。我已經嘗試將日期字段更改爲charfield,並且當我站起來一個新的posgresdb時得到相同的錯誤。 – grigs

+0

我想通了。事實證明,我的所有舊migration.py文件都將我在Heroku上的初始遷移拋出。我刪除了我的應用程序中的舊遷移文件。我的應用程序和一切都按原樣運行。我會添加答案。 – grigs