南方迁移失败

0 投票
1 回答
523 浏览
提问于 2025-04-17 10:42

我在使用south迁移时遇到了问题。我还是不太明白这是怎么发生的,以及我应该怎么做才能解决这个问题。

Romans-MacBook-Pro:holms$ ./manage.py migrate 
cRunning migrations for accounts:
- Nothing to migrate.
 - Loading initial data for accounts.
No fixtures found.
Running migrations for allocations:
- Nothing to migrate.
 - Loading initial data for allocations.
No fixtures found.
Running migrations for adyen:
- Nothing to migrate.
 - Loading initial data for adyen.
No fixtures found.
Running migrations for blog:
- Nothing to migrate.
 - Loading initial data for blog.
No fixtures found.
Running migrations for offers:
- Nothing to migrate.
 - Loading initial data for offers.
No fixtures found.
Running migrations for orders:
 - Migrating forwards to 0011_update_price_fields.
 > orders:0002_update_order_contact_information
Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_manager(settings)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/Users/holms/Development/xxx/xxx/settings/../../lib/south/management/commands/migrate.py", line 105, in handle
    ignore_ghosts = ignore_ghosts,
  File "/Users/holms/Development/xxx/xxx/settings/../../lib/south/migration/__init__.py", line 191, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/Users/holms/Development/xxx/xxx/settings/../../lib/south/migration/migrators.py", line 221, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/Users/holms/Development/xxx/xxx/settings/../../lib/south/migration/migrators.py", line 292, in migrate_many
    result = self.migrate(migration, database)
  File "/Users/holms/Development/xxx/xxx/settings/../../lib/south/migration/migrators.py", line 125, in migrate
    result = self.run(migration)
  File "/Users/holms/Development/xxx/xxx/settings/../../lib/south/migration/migrators.py", line 99, in run
    return self.run_migration(migration)
  File "/Users/holms/Development/xxx/xxx/settings/../../lib/south/migration/migrators.py", line 81, in run_migration
    migration_function()
  File "/Users/holms/Development/xxx/xxx/settings/../../lib/south/migration/migrators.py", line 57, in <lambda>
    return (lambda: direction(orm))
  File "/Users/holms/Development/xxx/migrations/0002_update_order_contact_information.py", line 29, in forwards
    for o in orders:
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 107, in _result_iter
    self._fill_cache()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 772, in _fill_cache
    self._result_cache.append(self._iter.next())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 273, in iterator
    for row in compiler.results_iter():
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: no such column: orders_order.pre_paid

这是导致问题的迁移文件的一部分:[0002_update_order_contact_information.py]:

# encoding: utf-8
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models

class Migration(DataMigration):

    def forwards(self, orm):
        Order = models.get_model('orders', 'Order')

        orders = Order.all_objects.select_related('buyer')
        orders = orders.filter(first_name__isnull=True)
        orders = orders.filter(buyer__isnull=False)
        orders = orders.exclude(payment_status="UNFINISHED")

        userfields = (
            'gender', 'birth_date', 'first_name', 'last_name', 'street_number',

1 个回答

2

你不应该直接和你的模型进行这样的交互。你使用的是 django.models,但这个版本的模型状态是错误的。你想要的是模型在迁移 0002 时的状态。根据 south 手册 的说明,你应该通过 orm 参数来访问你的模型。

注意,我们使用 orm.User 来访问用户模型 - 这样可以让我们得到在这个迁移创建时的用户版本,因此如果将来我们想要运行这个迁移,它就不会得到一个完全不同的新用户模型。来源

所以你应该这样重写迁移:

orders = orm.Order.objects.all()

或者甚至可以这样:

Order = orm.Order

撰写回答