Django模型南0.7.6迁移问题:表中的任何更改与另一个具有默认值百分比的字段

时间:2012-09-15 21:42:59

标签: django formatting default django-south

我在我的数据库模型中添加包含日期字段格式的字段后出现问题。

像这样:

date_format = models.CharField(max_length=32, default='%B %Y')

然而,在同一次迁移中,一些布尔值具有默认值。深入研究问题,并重建测试数据库......

问题在于表中的任何迁移,其中包含带有percentgae标志的默认值。所以不是在itselve中使用此字段进行迁移,而是迁移到同一个表(请参阅我的第二个答案)

添加如下字段:

show_category_name  = models.BooleanField(default=True)
使用auto schememigration

使用以下行提供迁移文件:

 # -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):

    def forwards(self, orm):
        # Adding field 'NewsItemCategory.show_category_name'
        db.add_column('iamweb_newsitemcategory', 'show_category_name',
                      self.gf('django.db.models.fields.BooleanField')(default=True),
                      keep_default=False)

给出了这个问题:

    bin/django migrate iamwebdjango.db.backends DEBUG (0.006) SELECT "south_migrationhistory"."id", "south_migrationhistory"."app_name", "south_migrationhistory"."migration", "south_migrationhistory"."applied" FROM "south_migrationhistory" WHERE "south_migrationhistory"."applied" IS NOT NULL ORDER BY "south_migrationhistory"."applied" ASC; args=()
Running migrations for iamweb:
 - Migrating forwards to 0020_auto__add_field_newsitemcategory_show_category_name__add_field_weblink.
 > iamweb:0020_auto__add_field_newsitemcategory_show_category_name__add_field_weblink
django.db.backends DEBUG (0.277) CREATE TABLE ROLLBACK_TEST (X INT); args=()
django.db.backends DEBUG (0.008) INSERT INTO ROLLBACK_TEST (X) VALUES (8); args=()
django.db.backends DEBUG (0.001) SELECT COUNT(X) FROM ROLLBACK_TEST; args=()
django.db.backends DEBUG (0.273) DROP TABLE ROLLBACK_TEST; args=()
django.db.backends DEBUG (0.177) CREATE TABLE STDDEV_TEST (X INT); args=()
django.db.backends DEBUG (0.000) SELECT STDDEV(*) FROM STDDEV_TEST; args=()
django.db.backends DEBUG (0.160) DROP TABLE STDDEV_TEST; args=()
django.db.backends DEBUG (0.151) CREATE TABLE DDL_TRANSACTION_TEST (X INT); args=()
django.db.backends DEBUG (0.000) CREATE TABLE DDL_TRANSACTION_TEST (X INT); args=()
django.db.backends DEBUG (0.155) DROP TABLE DDL_TRANSACTION_TEST; args=()
django.db.backends DEBUG (0.000) PRAGMA table_info("iamweb_newsitemcategory"); args=()
django.db.backends DEBUG (0.000) PRAGMA index_list("iamweb_newsitemcategory"); args=()
django.db.backends DEBUG (0.000) PRAGMA index_info("sqlite_autoindex_iamweb_newsitemcategory_1"); args=()
django.db.backends DEBUG (0.000) PRAGMA index_list("iamweb_newsitemcategory"); args=()
django.db.backends DEBUG (0.000) PRAGMA index_info("sqlite_autoindex_iamweb_newsitemcategory_1"); args=()
django.db.backends DEBUG (0.000) PRAGMA table_info("iamweb_newsitemcategory"); args=()
south DEBUG execute "CREATE TABLE "_south_new_iamweb_newsitemcategory" ("date_format" varchar(32) DEFAULT '%B %Y', "show_category_name" bool NOT NULL DEFAULT 1, "id" integer PRIMARY KEY, "menu_index" integer NOT NULL UNIQUE, "name" varchar(64) NOT NULL)" with params "[]"
 ! 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:
 ! The South developers regret this has happened, and would
 ! like to gently persuade you to consider a slightly
 ! easier-to-deal-with DBMS (one that supports DDL transactions)
 ! NOTE: The error which caused the migration to fail is further up.
Error in migration: iamweb:0020_auto__add_field_newsitemcategory_show_category_name__add_field_weblink
Traceback (most recent call last):
  File "bin/django", line 33, in <module>
    djangorecipe.manage.main('palmrif.developmentsettings')
  File "/media/storage/django/sites/palmrif/eggs/djangorecipe-0.20-py2.7.egg/djangorecipe/manage.py", line 16, in main
    management.execute_manager(mod)
  File "/media/storage/django/sites/palmrif/parts/django/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/media/storage/django/sites/palmrif/parts/django/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/media/storage/django/sites/palmrif/parts/django/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/media/storage/django/sites/palmrif/parts/django/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/management/commands/migrate.py", line 108, in handle
    ignore_ghosts = ignore_ghosts,
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/migration/__init__.py", line 213, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/migration/migrators.py", line 235, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/migration/migrators.py", line 310, in migrate_many
    result = self.migrate(migration, database)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/migration/migrators.py", line 133, in migrate
    result = self.run(migration)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/migration/migrators.py", line 107, in run
    return self.run_migration(migration)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/migration/migrators.py", line 81, in run_migration
    migration_function()
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/migration/migrators.py", line 57, in <lambda>
    return (lambda: direction(orm))
  File "/media/storage/django/sites/palmrif/local_checkouts_dev/iamweb/iamweb/migrations/0020_auto__add_field_newsitemcategory_show_category_name__add_field_weblink.py", line 14, in forwards
    keep_default=False)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/db/sqlite3.py", line 31, in add_column
    field.column: self._column_sql_for_create(table_name, name, field, False),
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/db/generic.py", line 44, in _cache_clear
    return func(self, table, *args, **opts)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/db/sqlite3.py", line 103, in _remake_table
    ", ".join(["%s %s" % (self.quote_name(cname), ctype) for cname, ctype in definitions.items()]),
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/db/generic.py", line 273, in execute
    cursor.execute(sql, params)
  File "/media/storage/django/sites/palmrif/parts/django/django/db/backends/util.py", line 38, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "/media/storage/django/sites/palmrif/parts/django/django/db/backends/__init__.py", line 505, in last_executed_query
    return smart_unicode(sql) % u_params
TypeError: not enough arguments for format string

我正在使用South 0.7.6。奇怪的是,这个版本应该修复了这个bug,请参阅:Release notes 0.7.6. bug fix default boolean value in sqlite

使用sqlite3,python 2.7.2,django 1.3.1(顺便说一句,在生产中使用mySql,但sqlite中的测试数据库非常方便)

请注意,问题是引用迁移文件中的第14行,即此行:

        db.add_column('iamweb_newsitemcategory', 'show_category_name',
                  self.gf('django.db.models.fields.BooleanField')(default=True),
                  keep_default=False)

没有足够的论据......对于初学者来说:我没有创造这条线,南方确实:)

堆栈跟踪有点令人困惑,因为调试行的最后一行(在堆栈跟踪之前是:

south DEBUG execute "CREATE TABLE "_south_new_iamweb_newsitemcategory" ("date_format" varchar(32) DEFAULT '%B %Y', "show_category_name" bool NOT NULL DEFAULT 1, "id" integer PRIMARY KEY, "menu_index" integer NOT NULL UNIQUE, "name" varchar(64) NOT NULL)" with params "[]"

所以它可能与South仍然想要解析字符串因为这个执行命令中的'%B%Y'....

经过4天的努力解决这个问题(当然不是全职)我开始怀疑南方的方式......但是,找不到更好的选择。

3 个答案:

答案 0 :(得分:1)

我刚才在南方邮件列表上回答。总结一下:布尔是无辜的,百分比是有罪的,Sqlite是一个玩具,因此它的南后端有点疯狂,显然有一个bug。一个新的,即将推出的South版本应避免在数据库中存储默认值,这可能会解决您的问题(至少对于测试而言)。

机票#317无关紧要。现在的票证#1192应该由当前处于拉取请求中的代码修复(或者你可以从我的前叉https://bitbucket.org/shaib/south中选择它)。

答案 1 :(得分:0)

问题可能不在'%B%Y'默认值中。 (因此我对问题和标题进行了编辑) sqlite中的默认布尔值似乎存在一些问题。请参阅我在问题中的编辑。 并且布尔值不支持默认值True和false。 见South ticket

迁移文件中的建议解决方案是:

 if db.backend_name == "sqlite3":
   default = 1
else:
   default = True
db.add_column('node_entry', 'visible', self.gf('django.db.models.fields.BooleanField')(default=default), keep_default=False)

但是这不起作用,进一步阅读,它应该在我正在使用的南方版本中得到解决......

答案 2 :(得分:0)

很明确问题是默认值'%B%Y'。 那个迁移很顺利,但在此之后新的迁移出错了。 我发现因为我有3个字段的布尔我想要默认为真。 其中2个在另一个领域的这个百分比的表中。 我标记了这两个迁移,剩下的只剩下一个了。

问题在于使用百分比符号重新创建表格

见曲目:

   File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/db/sqlite3.py", line 31, in add_column
    field.column: self._column_sql_for_create(table_name, name, field, False),
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/db/generic.py", line 44, in _cache_clear
    return func(self, table, *args, **opts)
  File "/media/storage/django/sites/palmrif/eggs/South-0.7.6-py2.7.egg/south/db/sqlite3.py", line 103, in _remake_table
    ", ".join(["%s %s" % (self.quote_name(cname), ctype) for cname, ctype in definitions.items()]),

所以我想我会尝试重新打开南票并将他们引荐到这个网站。 并更改此票的标题。

我在故障单上添加了评论:http://south.aeracode.org/ticket/317