我有这样的表:
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
表格与Positions
和UsersDevices
相关联。当我尝试通过管理员从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)
。可能是什么原因造成的?