GeoDjango & MySQL: points can't be NULL, what other “empty” value should I use?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 18:23:12

问题


I have this Django model:

from django.contrib.gis.db import models

class Event(models.Model):
    address = models.TextField()
    point = models.PointField('coordinates', null=True, blank=True)

When I sync this model using MySQL, this error message is printed while creating indexes:

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

so, not being able to use null=True (given that I want to have that index), what other possibilities do I have? I could define the point (0,0) as "empty", but then I have to remember that convention everywhere I plan to use the data, otherwise a whole lot of events will take place somewhere in the Atlantic west of Africa...

What other possibilities are there?


回答1:


I don't think you have many options here. Either you use a placeholder such as (0, 0), or you encapsulate the point in an object and reference the object everywhere you need the point. That way the reference to the object could be made nullable, but the extra join would hurt performance and complicate things.




回答2:


A quick idea would be to have another boolean field that you mark as true/false when you have an actual point. It would make querying easy because you could add the boolean field to the where clause.

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:


I just had the same issue. For me, I needed to specify to not make a spatial_index. So your model would change to:

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)


来源:https://stackoverflow.com/questions/4085785/geodjango-mysql-points-cant-be-null-what-other-empty-value-should-i-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!