“不能转换为类型整数”错误 [英] "cannot be cast to type integer" error

查看:212
本文介绍了“不能转换为类型整数”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,所以我会感激耐心。

This is my first question so I will appreciate patience.

我将一些属性从CharField更改为IntegerField。下面列出的代码如下:

I changed a few attributes to IntegerField from CharField. Listed below is the code:

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迁移,这是我得到的错误:

I did a south migration to migrate on Postgres and this is the error I get:

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

任何帮助将非常感谢,

Any help will be much appreciated and thank you in advance!!

推荐答案

如果您愿意丢弃数据,可以删除该列并创建新列

if you are happy to throw away your data, you can delete the column and create a new one

如果你想保存你的数据,你需要

if you want to keep your data, you need to either

a)给你的新列一个不同的名字, br>
b)创建一个临时列以在转换期间保存数据

a) give your new column a different name, or
b) create a temporary column to hold the data during the transition

您需要一系列迁移


  1. 用于添加新(或临时)列的模式迁移

  2. 显式移动数据的数据迁移, A - > 1)

  3. 可能是删除临时列的模式迁移

这篇关于“不能转换为类型整数”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆