Django表删除Integrityerror上的结果

时间:2014-04-29 09:14:42

标签: django postgresql django-models django-admin

我有这样的表:

class Positions(models.Model):
    address = models.CharField(max_length=255, blank=True)
    device = models.ForeignKey('Devices', blank=True, null=True)
    class Meta:
        db_table = 'positions'
        verbose_name = 'Positions'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return '%s' %(self.address)

class UsersDevices(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    devices = models.ForeignKey('Devices')
    class Meta:
        db_table = 'users_devices'
        verbose_name = 'User Devices'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return '%s %s' %(self.user, self.devices)


class Devices(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)

    latestposition = models.ForeignKey('Positions', db_column='latestPosition_id'.lower(), blank=True, null=True) # Field name made lowercase.
    class Meta:
        db_table = 'devices'
        verbose_name = 'Devices'
        verbose_name_plural = verbose_name

Device表格与PositionsUsersDevices相关联。当我尝试通过管理员从Device删除设备时,出现此错误:

Exception Type: IntegrityError at /admin/gps/devices/
Exception Value: update or delete on table "devices" violates foreign key constraint "devices_fkey" on table "positions"
DETAIL:  Key (id)=(100) is still referenced from table "positions".

当我从shell删除相同的设备时: Devices.objects.filter(pk=100).delete()没有错误消息。我的admin.py只是admin.site.register(model_name)。可能是什么原因造成的?

0 个答案:

没有答案