代码之家  ›  专栏  ›  技术社区  ›  Benjamin Wohlwend

geodjango&mysql:points不能为空,我应该使用其他什么“空”值?

  •  6
  • Benjamin Wohlwend  · 技术社区  · 14 年前

    我有这个姜戈模型:

    from django.contrib.gis.db import models
    
    class Event(models.Model):
        address = models.TextField()
        point = models.PointField('coordinates', null=True, blank=True)
    

    使用MySQL同步此模型时,创建索引时会打印此错误消息:

    Failed to install index for events.Event model: (1252, 'All parts of a SPATIAL index must be NOT NULL')
    

    所以,不能使用 null=True (考虑到我想要这个指数),我还有其他的可能性吗?我可以将点(0,0)定义为“空”,但是我必须记住,在我计划使用数据的任何地方,都要遵守惯例,否则,在非洲西部的大西洋某个地方将发生大量事件……

    还有其他的可能性吗?

    3 回复  |  直到 10 年前
        1
  •  2
  •   Gintautas Miliauskas    14 年前

    我认为你在这里没有很多选择。要么使用占位符(0,0),要么将点封装到对象中,并在需要点的任何位置引用对象。这样,对对象的引用可以设置为可以为空,但是额外的连接会损害性能并使事情复杂化。

        2
  •  0
  •   Astra    11 年前

    一个快速的想法是,当你有一个实际的点时,另一个布尔字段被标记为真/假。这将使查询变得容易,因为您可以将布尔字段添加到WHERE子句中。

    class Event(models.Model):
        ...
        has_point = models.BooleanField(default=False)
        point = models.PointField('coordinates', ...)
    
    Event.objects.filter(has_point=True)...#whatever else you need when handling location
    
        3
  •  0
  •   birdsarah    10 年前

    我也有同样的问题。对于我来说,我需要指定不创建空间索引。因此,您的模型将更改为:

    from django.contrib.gis.db import models
    
    class Event(models.Model):
        address = models.TextField()
        point = models.PointField('coordinates', null=True, blank=True, spatial_index=False)