django:'python manage.py migrate' 花费数小时(以及其他奇怪的行为)
django: 'python manage.py migrate' taking hours (and other weird behavior)
我对 models.py 中的一个 table 进行了一些更改,并尝试使用 'python manage.py migrate,' 迁移它,这需要几个小时。我只改了三个字段(列)的名字,到现在已经运行宁了2个多小时了。当我今天早上创建 table 时,它 运行 在几分钟内顺利(我认为)。赛季开始是做出改变的模型。
这是 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 class 看起来像这样(注意唯一的区别是最后 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 table:
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 has no attribute start_season" 这很奇怪,因为我从未尝试在我的脚本中访问该属性,只有 "start_of_season.start_season_pentad"
然后我想,好吧,我将更改字段的名称,以便 start_of_season 具有该属性。那时我将 models.py 编辑为看起来像 post 顶部的摘录。
更新更新后 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...
我不明白为什么这会花这么长时间,因为创建实际的 table 却没有。我也不确定为什么会出现其他错误。关于发生的事情有什么想法吗?
谢谢
这通常是因为有另一个 SQL 客户端或 django 服务器或 shell reading/writing 到 table 您正试图修改。
在访问 table 时无法成功执行更改架构的 SQL 命令。理想情况下应该弹出一条错误消息,但通常发生的是该命令只是等待其他命令完成。
一旦您关闭所有打开的 shell 和 sql 客户端,就会发生模式更改。在最坏的情况下,您可能还需要暂时将网站下线。
我对 models.py 中的一个 table 进行了一些更改,并尝试使用 'python manage.py migrate,' 迁移它,这需要几个小时。我只改了三个字段(列)的名字,到现在已经运行宁了2个多小时了。当我今天早上创建 table 时,它 运行 在几分钟内顺利(我认为)。赛季开始是做出改变的模型。
这是 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 class 看起来像这样(注意唯一的区别是最后 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 table:
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 has no attribute start_season" 这很奇怪,因为我从未尝试在我的脚本中访问该属性,只有 "start_of_season.start_season_pentad"
然后我想,好吧,我将更改字段的名称,以便 start_of_season 具有该属性。那时我将 models.py 编辑为看起来像 post 顶部的摘录。
更新更新后 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...
我不明白为什么这会花这么长时间,因为创建实际的 table 却没有。我也不确定为什么会出现其他错误。关于发生的事情有什么想法吗?
谢谢
这通常是因为有另一个 SQL 客户端或 django 服务器或 shell reading/writing 到 table 您正试图修改。
在访问 table 时无法成功执行更改架构的 SQL 命令。理想情况下应该弹出一条错误消息,但通常发生的是该命令只是等待其他命令完成。
一旦您关闭所有打开的 shell 和 sql 客户端,就会发生模式更改。在最坏的情况下,您可能还需要暂时将网站下线。