Django South:自动模式迁移不适用于添加ManyToMany

时间:2014-06-11 04:23:13

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

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

我有一个模型,我正在尝试向其添加一个额外的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'
        )

该应用程序名为' profile'。首先我这样做了:

$ 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)

好。现在应该正常工作吗?

嗯,它似乎确实奏效了。我跑了一些测试......

>>> 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

有人可以了解这里发生的事情吗?

1 个答案:

答案 0 :(得分:0)

首先,如果您运行syncdb,那么已经迁移的应用将被忽略,这就是他们未同步的原因。

第二个问题不在南方,它完成了迁移,你应该检查你的数据库,只是为了确保它是否创建了直通表(&#39; appname_userprofile_geofence&#39;,或类似的东西),我确信它确实如此:> profile:0007_auto,如果迁移尚未完成,DatabaseError也不会AttributeError

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