我对www.example.com中的一个表做了一些更改models.py,并尝试使用“pythonmanage.pymigrate”迁移它,但这需要几个小时。(列)名称,现在已经运行了两个多小时了,运行的很平稳(我想)在今天早上早些时候我创建表的几分钟内。赛季开始是做出改变的模型。
下面是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 does
就有了这个属性,于是我编辑了models.py,使其看起来像文章顶部的摘录。
在更新www.example.com之后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...
我不明白为什么这要花这么长时间,因为创建实际的表并没有花这么长时间。我也不知道为什么我会得到其他错误。你知道发生了什么吗?
谢谢
2条答案
按热度按时间lb3vh1jj1#
这通常是因为有另一个SQL客户端或django服务器或shell正在阅读/写您试图修改的表。
在访问表时,无法成功执行更改方案的SQL命令。理想情况下,应该弹出一条错误消息,但通常发生的情况是,该命令只是等待其他命令完成。
一旦你关闭了所有打开的shell和sql客户端,架构就会发生变化。在最坏的情况下,你可能需要暂时让网站离线。
3wabscal2#
对我来说,它甚至没有启动!我使用docker-compose来运行我的项目,我的Dockerfile包含
RUN python manage.py migrate
。结果我把debugpy代码放在www.example.com中manage.py,让它使用wait_for_client***等待调试器,而***没有在vscode中按调试按钮,这花了很长时间。根据debugpy文档(等待客户端连接)
使用
debugpy.wait_for_client()
函数阻止程序执行,直到连接客户端。我想做的是:How to debug Django in VSCode with autoreload turned on