2013-01-06 30 views
8

這是我的第一個問題,所以我會感激耐心。「不能投入類型整數」錯誤

我從CharField將一些屬性改爲IntegerField。下面列出的是代碼:

rating_choices = (
    (1,"1"), 
    (2,"2"), 
    (3,"3"), 
    (4,"4"), 
    (5,"5"), 
) 

class Rating(models.Model): 
    article = models.ForeignKey(Article,null=True) 
    organization = models.IntegerField(choices=rating_choices, default=1) 
    support = models.IntegerField(choices=rating_choices, default=1) 
    readability = models.IntegerField(choices=rating_choices, default=1) 
    tags = models.IntegerField(choices=rating_choices, default=1) 
    comments = models.TextField() 
    def get_overall_rating(self): 
     return fsum(self.organization + self.support + self.support + self.readability + self.tags)/5.0 
    overall_rating = property(get_overall_rating) 
admin.site.register(Rating) 

我做了一個南方遷移遷移對Postgres的,這是我得到的錯誤:

Error in migration: collect_data:0010_auto__chg_field_rating_tags__chg_field_rating_support__chg_field_ratin 
Traceback (most recent call last): 
    File "manage.py", line 15, in <module> 
    execute_from_command_line(sys.argv) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line 
    utility.execute() 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute 
    output = self.handle(*args, **options) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/management/commands/migrate.py", line 108, in handle 
    ignore_ghosts = ignore_ghosts, 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/migration/__init__.py", line 213, in migrate_app 
    success = migrator.migrate_many(target, workplan, database) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/migration/migrators.py", line 235, in migrate_many 
    result = migrator.__class__.migrate_many(migrator, target, migrations, database) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/migration/migrators.py", line 310, in migrate_many 
    result = self.migrate(migration, database) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate 
    result = self.run(migration) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/migration/migrators.py", line 107, in run 
    return self.run_migration(migration) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/migration/migrators.py", line 81, in run_migration 
    migration_function() 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/migration/migrators.py", line 57, in <lambda> 
    return (lambda: direction(orm)) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellow/jellow/apps/collect_data/migrations/0010_auto__chg_field_rating_tags__chg_field_rating_support__chg_field_ratin.py", line 13, in forwards 
    db.alter_column('collect_data_rating', 'tags', self.gf('django.db.models.fields.IntegerField')()) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/db/generic.py", line 44, in _cache_clear 
    return func(self, table, *args, **opts) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/db/generic.py", line 522, in alter_column 
    flatten(values), 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/south/db/generic.py", line 273, in execute 
    cursor.execute(sql, params) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute 
    return self.cursor.execute(sql, params) 
    File "/Users/narensathiya/Documents/Documents/Jellow/Jellow/jellowenv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute 
    return self.cursor.execute(query, args) 
django.db.utils.DatabaseError: column "tags" cannot be cast to type integer 

任何幫助將非常感激,並預先感謝您!

+0

是否有已經不在整數形式的任何項目在'tags'列在你的數據庫? –

+0

是的,應該有。我已經將一些值保存爲字符。我是否必須刪除該列並重新制作它? – Badi8beach

回答

17

,如果你高興地扔掉你的數據,你可以刪除列,並創建一個新的

,如果你想保持你的數據,你需要或者

一)給你的新列不同的名稱,或
二)建立一個臨時列在過渡期間保持數據

然後需要遷移的序列

  1. 進行模式遷移以添加新(或臨時)列
  2. 顯式移動數據的數據遷移,執行任何必需的轉換(例如, 「A」 - > 1)
  3. 可能模式遷移刪除您的臨時列
+0

酷我沒有做過數據遷移,所以我會試試看! – Badi8beach

1

您需要添加 「使用(COL_NAME ::整數)」 來消除這個錯誤。 但在這種情況下,你必須使用直接查詢。

migrateEngine.execute( 'ALTER TABLE測試ALTER COLUMN testScore Integer類型使用testScore ::整數')

相關問題