【问题标题】:Django/South "python manage.py migrate CaseReport" raises an exceptionDjango/South“python manage.py migrate CaseReport”引发异常
【发布时间】:2014-05-28 17:15:10
【问题描述】:

我正在尝试将 South 支持添加到现有的 Django 应用程序中。

我做了什么:

pip install south
Add 'south' to INSTALLED_APPS in settings.py

Test that South is now there:
$ python manage.py shell
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import south
>>>

Also, check that South shows up in manage.py help:

python manage.py help

python manage.py syncdb

Make south manage your models:

python manage.py convert_to_south CaseReport

On other instances:
manage.py migrate app_name 0001 --fake

Make your model changes in models.py

Check what -would- happen (dry run):

python manage.py schemamigration CaseReport --auto --stdout

Run it for real:
python manage.py schemamigration CaseReport --auto

Finally:
python manage.py migrate CaseReport
...but this is tracebacking for me.  :(

我得到的回溯是:

$ python manage.py migrate CaseReport
Running migrations for CaseReport:
 - Migrating forwards to 0002_auto__add_field_casereport_date_time_of_last_update.
 > CaseReport:0002_auto__add_field_casereport_date_time_of_last_update
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/south/management/commands/migrate.py", line 111, in handle
    ignore_ghosts = ignore_ghosts,
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/south/migration/__init__.py", line 220, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/south/migration/migrators.py", line 254, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/south/migration/migrators.py", line 329, in migrate_many
    result = self.migrate(migration, database)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate
    result = self.run(migration, database)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/south/migration/migrators.py", line 111, in run
    if not south.db.db.has_ddl_transactions:
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/django/utils/functional.py", line 49, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/south/db/generic.py", line 124, in has_ddl_transactions
    if getattr(connection.features, 'supports_transactions', True):
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/django/utils/functional.py", line 49, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 676, in supports_transactions
    self.connection.leave_transaction_management()
  File "/home/dstromberg/miniconda/envs/CaseReport/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 322, in leave_transaction_management
    "Transaction managed block ended with pending COMMIT/ROLLBACK")
django.db.transaction.TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK

有谁知道我做错了什么?

我在 Linux Mint 16 上使用 Django 1.6 和 Miniconda CPython 2.7.5、South 0.8.4 和 MySQL 5.5.37-0ubuntu0.13.10.1。

谢谢!

【问题讨论】:

  • 我猜你事先使用过你的数据库并且有一个待处理的事务。也许尝试重新启动您的数据库服务器。另外,您的代码中有哪些部分是您手动处理事务管理的?
  • 我用“sudo service mysql restart”重启了mysql,但是在运行“python manage.py migrate CaseReport”时我仍然得到了回溯。不过还是谢谢你的建议。
  • 这很酷,但我评论的第二部分更重要:您的代码中是否有任何地方可以手动管理事务?
  • 我现在没有明确使用事务。
  • 这些模型长什么样?

标签: python django django-south


【解决方案1】:

我遇到了同样的问题,并花了数小时应用不同的解决方案。问题是 MySQL 用户可能没有操作表的权限或数据库中存在 ROLLBACK_TEST 表。我两者都有,所以也许它会对某人有所帮助:

授予用户所需的数据库权限,然后刷新权限以生效:

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX 
    ON `databasename`.* TO 'username'@'ip.address' IDENTIFIED BY 'somepass';
FLUSH PRIVILEGES;
SHOW GRANTS FOR 'username'@'ip.address';

删除表 ROLLBACK_TEST(如果有):

USE databasename
DROP TABLE ROLLBACK_TEST;

然后迁移很顺利。

【讨论】:

  • 谢谢。我完全迷失了,我从没想过要查找其他用户拥有的表。
猜你喜欢
  • 1970-01-01
  • 2019-11-11
  • 2015-11-01
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
  • 2016-10-10
相关资源
最近更新 更多