Django + spatialite migration IntegrityError
When I modify a class/table with PolygonField, the migration stops with the following error:
django.db.utils.IntegrityError: The row in table 'geometry_columns_statistics' with primary key 'new__mc_sitio_benfeitoria' has an invalid foreign key: geometry_columns_statistics.f_table_name contains a value 'new__mc_sitio_benfeitoria' that does not have a corresponding value in geometry_columns.f_table_name.
My app is MC_SITIO and the class follows:
class ImovelBase(ModelGis):
"""Base para Imóveis"""
descricao = CharField(max_length=255, blank=False, null=True, verbose_name='descrição')
administrador = ForeignKey(Pessoa, on_delete=SET_NULL, blank=True, null=True)
RIP = CharField(max_length=255, blank=True, null=True, help_text='se for imóvel da União')
codigo = CharField(max_length=255, blank=True, null=True, verbose_name='código, matricula, anúncio, ou outro documento')
endereco = ForeignKey(Endereco, on_delete=SET_NULL, blank=True, null=True, verbose_name = 'endereço')
poligono = PolygonField(srid=EPSG_SRID_PADRAO, blank=True, null=True, verbose_name='polígono')
# area_total = UnumCharField(valid_unit=m**2, max_length=255, null=True, blank=True, verbose_name='área total', validators = [MinValueValidator(0.0)])
area_total = SympyCharField(max_length=255, null=True, blank=True, verbose_name='área total')
VUP = DecimalField(null=True, blank=True, verbose_name='valor unitário padrão (VUP)', max_digits=11, decimal_places=2, validators = [MinValueValidator(0.0)])
indice_fiscal = DecimalField(null=True, blank=True, verbose_name='índice fiscal', max_digits=11, decimal_places=2, validators = [MinValueValidator(0.0)])
em_condominio = BooleanField(default=False, blank=True, verbose_name='em condomínio')
observacoes = TextField(null=True, blank=True, verbose_name='observações')
class Meta:
abstract = True
def __str__(self):
return "{0}".format(self.descricao)
def area(self):
if self.poligono:
return self.poligono.transform(get_SRID_UTM_WGS84(self.poligono.centroid), clone=True).area*m**2
return "S/N"
@property
def popupConteudo(self):
return '<a href="/terreno-{}.html" target="_blank">{}</a>'.format(self.id, self.descricao)
class TerrenoBase(ImovelBase):
"""Base para Terreno, tombo"""
relevo = CharField(choices=RelevoClasse.choices, max_length=255, blank=True, null=True, verbose_name='classe do relevo')
forma_regular = BooleanField(default=False, blank=True, verbose_name='forma regular?')
esquinas_qtd = PositiveSmallIntegerField(blank=True, null=True, verbose_name="quantidade de esquinas")
# testada = UnumCharField(valid_unit=m, max_length=255, null=True, blank=True)
testada = SympyCharField(max_length=255, null=True, blank=True)
testadas_qtd = PositiveSmallIntegerField(blank=True, null=True, verbose_name="quantidade de testadas")
# gabarito = UnumCharField(valid_unit=m, max_length=255, null=True, blank=True)
gabarito = SympyCharField(max_length=255, null=True, blank=True)
macrozoneamento = CharField(max_length=255, null=True, blank=True)
zoneamento = CharField(max_length=255, null=True, blank=True)
tombamentos = ManyToManyField(Referencia, blank=True, verbose_name='tombamento(s)')
class Meta:
abstract = True
class Terreno(TerrenoBase):
"""Terreno, tombo"""
pass
class Benfeitoria(ImovelBase):
"""Benfeitoria, casa, prédio, galpão, ponte, edificação, ou qualquer outra construção permanente sobre um terreno"""
terreno = ForeignKey(Terreno, on_delete=SET_NULL, blank=False, null=True)
tipob = PositiveSmallIntegerField(choices=BenfeitoriaTipo.choices, blank=False, null=True, verbose_name='tipo da benfeitoria')
pav_desc = CharField(max_length=255, null=True, blank=True, verbose_name='descrição dos pavimentos', help_text='T: térreo, PT: pav. tipo, LJ: loja, C: cobertura, etc')
rr_desc = CharField(choices=RossHeidecke.choices, max_length=255, null=True, blank=True, verbose_name='Critério de Ross-Heidecke')
vida_util = CharField(choices=BenfeitoriaVidaUtilPadrao.choices, max_length=255, blank=True, null=True, verbose_name='vida útil padrão')
How to solve it?