如何在Django中更新列数据类型

时间:2010-07-01 12:02:53

标签: django django-south

我有新的python和django我在使用“South”创建模型轮询和选择后创建轮询应用程序并在该应用程序中。我想将问题字段的长度从200改为300,但即使使用南方也无法实现。

我运行了python manage.py schemamigration polls --initial命令来创建迁移文件然后我在民意调查问题字段中进行更改(问题= models.CharField(max_length = 250))将max_length从200更改为250。 / p>

再次运行python manage.py schemamigration民意调查 - 自动生成新的迁移文件。

之后我运行的所有东西都是python manage.py migrate polls,它显示以下错误:

C:\Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated
  from sets import ImmutableSet
Running migrations for polls:
 - Migrating forwards to 0003_auto__chg_field_poll_question.
 > polls:0003_auto__chg_field_poll_question
 ! Error found during real run of migration! Aborting.

 ! Since you have a database that does not support running
 ! schema-altering statements in transactions, we have had
 ! to leave it in an interim state between migrations.

! You *might* be able to recover with:   = ALTER TABLE `polls_poll` ; []
   = ALTER TABLE `polls_poll` MODIFY `question` varchar(200) NOT NULL;; []
   = ALTER TABLE `polls_poll` ALTER COLUMN `question` DROP DEFAULT; []

 ! The South developers regret this has happened, and would
 ! like to gently persuade you to consider a slightly
 ! easier-to-deal-with DBMS.
 ! NOTE: The error which caused the migration to fail is further up.
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
362, in execute_manager
    utility.execute()
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
303, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 195,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 222,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python26\lib\site-packages\south\management\commands\migrate.py", lin
e 109, in handle
    ignore_ghosts = ignore_ghosts,
  File "C:\Python26\lib\site-packages\south\migration\__init__.py", line 202, in
 migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "C:\Python26\lib\site-packages\south\migration\migrators.py", line 220, i
n migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, datab
ase)
  File "C:\Python26\lib\site-packages\south\migration\migrators.py", line 291, i
n migrate_many
    result = self.migrate(migration, database)
  File "C:\Python26\lib\site-packages\south\migration\migrators.py", line 125, i
n migrate
    result = self.run(migration)
  File "C:\Python26\lib\site-packages\south\migration\migrators.py", line 99, in
 run
    return self.run_migration(migration)
  File "C:\Python26\lib\site-packages\south\migration\migrators.py", line 81, in
 run_migration
    migration_function()
  File "C:\Python26\lib\site-packages\south\migration\migrators.py", line 57, in
 <lambda>
    return (lambda: direction(orm))
  File "C:\mysite\..\mysite\polls\migrations\0003_auto__chg_field_poll_question.
py", line 12, in forwards
    db.alter_column('polls_poll', 'question', self.gf('django.db.models.fields.C
harField')(max_length=250))
  File "C:\Python26\lib\site-packages\south\db\generic.py", line 330, in alter_c
olumn
    self.delete_foreign_key(table_name, name)
  File "C:\Python26\lib\site-packages\south\db\generic.py", line 588, in delete_
foreign_key
    constraints = list(self._constraints_affecting_columns(table_name, [column],
 "FOREIGN KEY"))
  File "C:\Python26\lib\site-packages\south\db\mysql.py", line 140, in _constrai
nts_affecting_columns
    """, [db_name, table_name, type])
  File "C:\Python26\lib\site-packages\south\db\generic.py", line 134, in execute

    cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 19, in e
xecute
    return self.cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 84
, in execute
    return self.cursor.execute(query, args)
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 168, in execute
    if not self._defer_warnings: self._warning_check()
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 82, in _warning_
check
    warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: Can't find file: 'slow_log' (errno: 2)

请帮帮我

0003看起来像:

类迁移(SchemaMigration):

def forwards(self, orm):

    # Changing field 'Poll.question'
    db.alter_column('polls_poll', 'question', self.gf('django.db.models.fields.CharField')(max_length=250))


def backwards(self, orm):

    # Changing field 'Poll.question'
    db.alter_column('polls_poll', 'question', self.gf('django.db.models.fields.CharField')(max_length=200))

Ansh J

4 个答案:

答案 0 :(得分:2)

直接从mysql控制台修改mysql表!

python manage.py dbshell
alter table appname_modelname modify `question` varchar(200) NOT NULL;

答案 1 :(得分:1)

为了能够确定地回答这个问题,您需要显示迁移0002和0003 ......

然而,在我看来,产生的异常只是MySQL的一个问题(它无法找到它的“慢速日志”),它会创建一个传播到南方的警告,然后跳过它。

答案 2 :(得分:1)

你遇到的问题不在于Django或South,而是在MySQL中。 MySQL正在咳嗽如下:

_mysql_exceptions.Warning: Can't find file: 'slow_log' (errno: 2)

并且它正在恐慌MySQLdb库,它正在触发救助,即使它只是一个警告。

你需要找出为什么MySQL如此担心它丢失的slow_log文件。

答案 3 :(得分:0)

我怀疑你可能在其中有general_log和slow_log frm文件 mysql数据库目录,没有任何相应的数据文件。如果 这就是'rm'general_log.frm和slow_log.frm的情况 来自mysql数据库目录的文件,以及所有这些错误都应该去 程。