model修正后,makemigration → migrate时,报错
model修正内容:field追加、field修正(default='' ⇒ null=True)等
cannot alter table because it has pending trigger events
django.db.utils.OperationalError: 保留中のトリガーイベントがあるため"xx_table"をALTER TABLEできません
1,model定义中,删除任意字段
2,删除生成的migration file,重新执行 makemigration , migrate
model修正前:
appl.py 修正前
class Appl(BaseModel):
title = models.CharField(verbose_name='標題', max_length=200, null = True)
times = models.IntegerField(verbose_name='回数', default=0)
condition = models.CharField(verbose_name='条件', max_length=200, default="")
p_date = models.DateField(verbose_name='支払日', null = True)
resp = models.ForeignKey(User, verbose_name="担当者", on_delete=models.SET_NULL, null=True)
p_method = models.ForeignKey(Method, verbose_name='方法', on_delete=models.SET_NULL, null=True)
model 修正后:
appl.py 修正后
class Appl(BaseModel):
title = models.TextField(verbose_name='標題', max_length=200, null = True) # char→text
times = models.IntegerField(verbose_name='回数', null = True) # integer:default=0 → null = True
condition = models.CharField(verbose_name='条件', max_length=200, null = True) # char:default="" → null = True
p_date = models.DateField(verbose_name='支払日', null = True) # 未变更
resp = models.CharField(verbose_name="担当者", max_length=100, null=True) # FK→ char
makemigration , migrate 执行结果
(smsenv) PS D:\Projects\pj> python manage.py makemigrations pur
Migrations for 'purchase':
pur\migrations\0018_rename_apply_date_xxx_and_more.py
- Rename field apply_date on appl to apply_at
- Remove field accounting_manager from appl
略
(smsenv) PS D:\Projects\pj> python manage.py migrate pur
Operations to perform:
Apply all migrations: pur
Running migrations:
Applying pur.0018_rename_apply_date_xxx_and_more...Traceback (most recent call last):
File "D:\Projects\smsenv\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.ObjectInUse: 保留中のトリガーイベントがあるため"appl"をALTER TABLEできません
解决方法:
网上查了一些,很多说需要把数据移行,和schema移行分开处理
我试了,删除model中的任意字段,重新执行移行处理
1 删除移行文件
2,修改model:删除model中的任意字段
3,重新执行移行,成功
(smsenv) PS D:\Projects\pj> python manage.py makemigrations pur
Migrations for 'pur':
pur\migrations\0018_rename_apply_date_xxx_and_more.py
略
(smsenv) PS D:\Projects\pj> python manage.py migrate pur
Operations to perform:
Apply all migrations: pur
Running migrations:
Applying pur.0018_rename_apply_date_xxx_and_more... OK
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。