保留中のトリガーイベントがあるため"xx_table"をALTER TABLEできません

2022-07-26 22:54:22 浏览数 (1)

問題:

model修正后,makemigration → migrate时,报错

model修正内容:field追加、field修正(default='' ⇒ null=True)等

error内容:

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修正前:

代码语言:javascript复制
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 修正后:

代码语言:python代码运行次数:0复制
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 执行结果

代码语言:javascript复制
(smsenv) PS D:Projectspj> python manage.py makemigrations pur
Migrations for 'purchase':
  purmigrations018_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:Projectspj> 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:Projectssmsenvlibsite-packagesdjangodbbackendsutils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.ObjectInUse: 保留中のトリガーイベントがあるため"appl"をALTER TABLEできません

解决方法:

网上查了一些,很多说需要把数据移行,和schema移行分开处理

例如:link stack overflow

我试了,删除model中的任意字段,重新执行移行处理

1 删除移行文件

将migrations文件夹下,最新生成的移行文件删除:0018_rename_apply_date_xxx_and_more...将migrations文件夹下,最新生成的移行文件删除:0018_rename_apply_date_xxx_and_more...

2,修改model:删除model中的任意字段

删除 tax删除 tax

3,重新执行移行,成功

代码语言:javascript复制
(smsenv) PS D:Projectspj> python manage.py makemigrations pur
Migrations for 'pur':
  purmigrations018_rename_apply_date_xxx_and_more.py
  略

(smsenv) PS D:Projectspj> python manage.py migrate pur
Operations to perform:
  Apply all migrations: pur
Running migrations:
  Applying pur.0018_rename_apply_date_xxx_and_more... OK

0 人点赞