【问题标题】:Django South: Auto schemamigration didn't work for adding ManyToManyDjango South:自动架构迁移不适用于添加多对多
【发布时间】:2014-08-01 00:29:30
【问题描述】:

我对南方很陌生。我正在处理一个朋友项目,他似乎已经完成了一些迁移。

我有一个模型,我正在尝试向它添加一个额外的 ManyToMany 字段。这是owned_geofences 是我要添加的字段的类:

class UserProfile(models.Model):

    owned_beacons = models.ManyToManyField(
       Beacon,
       blank=True,
       null=True,
       related_name='owned_beacons'
       )

    #trying to add this one below
    owned_goefences = models.ManyToManyField(
        Geofence,
        blank=True,
        null=True,
        related_name='owned_geofences'
        )

该应用名为“个人资料”。首先我是这样做的:

$ sudo python manage.py schemamigration profile --auto
 + Added M2M table for owned_goefences on profile.UserProfile
Created 0007_auto.py. You can now apply this migration with: ./manage.py migrate profile

酷,它似乎奏效了。然后我这样做了:

$ python manage.py migrate profile
Running migrations for profile:
 - Migrating forwards to 0007_auto.
 > profile:0007_auto
 - Loading initial data for profile.
Installed 0 object(s) from 0 fixture(s)

好的。现在 It 应该可以正常工作了吗?

嗯,它似乎确实奏效了。我进行了一些测试,然后..

>>> user = User.objects.get(pk=1)
<User: nick>
# just to test if user is working properly
>>> user.get_profile().owned_beacons
<django.db.models.fields.related.ManyRelatedManager object at 0x1104e7d50>
# this is the one that isn't working
>>> user.get_profile().owned_geofences
AttributeError: 'UserProfile' object has no attribute 'owned_geofences'

最重要的是,出于好奇,我跑了:

$ python manage.py syncdb
Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.sites
 > django.contrib.messages
 > django.contrib.staticfiles
 > django.contrib.admin
 > django.contrib.admindocs
 > south

Not synced (use migrations):
 - pp.apps.careers
 - pp.apps.contact
 - pp.apps.geofencemanager
 - pp.apps.locationmanager
 - pp.apps.messagemanager
 - django_extensions
 - pp.profile
 - pp.apps.beaconmanager
(use ./manage.py migrate to migrate these)

为什么这些不同步?我以为我搞砸了,所以我接受了上面的建议并跑了:

$ python manage.py migrate
Running migrations for careers:
- Nothing to migrate.
#etc for all of them

有人可以解释一下这里发生了什么吗?

【问题讨论】:

    标签: django django-models migration django-south schema-migration


    【解决方案1】:

    首先如果你运行syncdb,已经有迁移的应用将被忽略,这就是它们不同步的原因。

    第二个问题不在南方,它完成了迁移,你应该检查你的数据库,只是为了确保它是否创建了直通表('appname_userprofile_geofence',或类似的东西),我确信它确实做到了因为它是这么说的:&gt; profile:0007_auto,如果没有完成迁移,它也会给你一个DatabaseError,而不是AttributeError

    此外,一切似乎都很好,请确保重新加载 shell,以便它更新了 UserProfile 模型的版本。

    【讨论】:

      猜你喜欢
      • 2011-03-11
      • 2012-03-22
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2018-10-12
      • 2014-11-07
      • 2012-05-19
      • 2012-08-06
      相关资源
      最近更新 更多