django:' python manage.py migrate'花费数小时(和其他奇怪的行为)

时间:2015-09-01 17:59:50

标签: python django django-models django-migrations

我对models.py中的一个表进行了一些更改,并尝试使用&py; manage.py migrate迁移它,'而且需要几个小时。我只更改了三个字段(列)名称的名称,现在它已经运行了2个多小时。今天早些时候创建这张桌子时,它在几分钟内就顺利运行了(我想)。季节开始是改变的模型。

以下是来自models.py的内容:

from django.db import models
from django.contrib.gis.db import models as gismodels
# from django.contrib.gis import admin

# Create your models here.

class Location(models.Model): # table name automatically chirps_location
    locationID = models.IntegerField(default=0, primary_key=True)
    lat = models.FloatField(default=0.0)
    lon = models.FloatField(default=0.0)
    geom = gismodels.PointField(null=True)
    objects = gismodels.GeoManager()
    def __unicode__(self):
        return u"LocationID: " + unicode(self.locationID)

# admin.site.unregister(Location)
# admin.site.register(Location, admin.OSMGeoAdmin)


class Rainfall(models.Model):
    location = models.ForeignKey(Location)
    year = models.IntegerField(default=0)
    pentad_num = models.IntegerField(default=0)        
    pentad_val = models.FloatField(default=0.0)

    class Meta:
        ordering = ['location', 'year', 'pentad_num']

    def __unicode__(self):
        return u"LocationID: " + unicode(self.location.locationID) + u" Year: " + unicode(self.year) + u" Pentad: " + unicode(self.pentad_num)


class Start_of_Season(models.Model):
    location = models.ForeignKey(Location)
    crop = models.CharField(default='', max_length=40)
    year = models.IntegerField(default=0)
    first_rain = models.IntegerField(default=0)
    onset_rain = models.IntegerField(default=0)
    start_season = models.IntegerField(default=0)

    class Meta:
        ordering = ['location', 'crop', 'year']    

    def __unicode__(self):
        return u"LocationID: " + unicode(self.location.locationID) + u" Year: " + unicode(self.year) + u" Start of Season pentad: " + unicode(self.start_season)

在此之前还有一些我无法解释的奇怪行为,所以如果它全部相关的话我也会简要介绍所有这些。

首先,我的Start_of_Season类看起来像这样(注意唯一的区别是最后3个字段的名称):

class Start_of_Season(models.Model):
    location = models.ForeignKey(Location)
    crop = models.CharField(default='', max_length=40)
    year = models.IntegerField(default=0)
    first_rain_pentad = models.IntegerField(default=0)
    onset_rain_pentad = models.IntegerField(default=0)
    start_season_pentad = models.IntegerField(default=0)

    class Meta:
        ordering = ['location', 'crop', 'year']    

    def __unicode__(self):
        return u"LocationID: " + unicode(self.location.locationID) + u" Year: " + unicode(self.year) + u" Start of Season pentad: " + unicode(self.start_season)

使用以下方式迁移它:

python manage.py makemigrations
python manage.py migrate

似乎运行顺利。

但是当我运行一个python脚本(摘录)来向这个新创建的Start_of_Season表中添加行时:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "access.settings")
from chirps.models import Location, Rainfall, Start_of_Season
django.setup()

try:
    with transaction.atomic():
        for loc in Location.objects.all():
            locID = loc.locationID
            for yr in range(1981, 2014):
                # some computations to find: c1, c2, c3
                start_of_season = Start_of_Season()
                start_of_season.location = Location.objects.get(locationID = locID)
                start_of_season.crop = 'millet'
                start_of_season.year = yr
                start_of_season.first_rain_pentad = c1
                start_of_season.onset_rain_pentad = c2
                start_of_season.start_season_pentad = c3
                start_of_season.save()

首先,行从未添加到数据库中(至少根据psql)。然后我收到错误" start_of_season没有属性start_season"这很奇怪,因为我从未试图在我的脚本中访问该属性,只有" start_of_season.start_season_pentad"

然后我想,好吧,我将更改字段的名称,以便start_of_season 具有该属性。当我编辑models.py看起来就像帖子顶部的摘录时那样。

更新models.py更新后,

python manage.py makemigrations

没有错误地运行:

(access_mw)mwooten@ip-10-159-67-226:~/dev/access$ python manage.py makemigrations
Did you rename start_of_season.first_rain_pentad to start_of_season.first_rain (a IntegerField)? [y/N] y
Did you rename start_of_season.onset_rain_pentad to start_of_season.onset_rain (a IntegerField)? [y/N] y
Did you rename start_of_season.start_season_pentad to start_of_season.start_season (a IntegerField)? [y/N] y
Migrations for 'chirps':
  0010_auto_20150901_1454.py:
    - Rename field first_rain_pentad on start_of_season to first_rain
    - Rename field onset_rain_pentad on start_of_season to onset_rain
    - Rename field start_season_pentad on start_of_season to start_season

,但是:

python manage.py migrate

已经被困在这里几个小时了:

(access_mw)mwooten@ip-10-159-67-226:~/dev/access$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, gis, djgeojson, messages, leaflet
  Apply all migrations: admin, chirps, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying chirps.0010_auto_20150901_1454...

我不知道为什么这需要这么长时间,看看创建实际表格的方式并不是这样。我也不确定为什么我会收到其他错误。关于发生了什么的任何想法?

由于

1 个答案:

答案 0 :(得分:6)

这通常是因为有另一个SQL客户端或django服务器或shell读取/写入您要修改的表。

在访问表时,无法成功执行更改架构的SQL命令。理想情况下,应该弹出一条错误消息,但通常发生的是该命令只是等待其他人完成。

关闭所有打开的shell和sql客户端后,可能会发生架构更改。在最糟糕的情况下,您可能还需要暂时使网站脱机。