GeoDjango 和MySQL:点不能为NULL,还有什么“空”?我应该使用的值?

发布于 2024-09-30 02:22:09 字数 544 浏览 4 评论 0原文

我有这个 Django 模型:

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) 定义为“空”,但是我必须记住我计划使用数据的所有地方的约定,否则很多事件将发生在非洲西部大西洋的某个地方......

还有哪些其他可能性?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心在旅行 2024-10-07 02:22:09

我认为你在这里没有太多选择。您可以使用占位符(例如 (0, 0)),也可以将该点封装在一个对象中,并在需要该点的任何地方引用该对象。这样,对对象的引用可以为空,但额外的连接会损害性能并使事情变得复杂。

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.

秋风の叶未落 2024-10-07 02:22:09

我刚刚遇到了同样的问题。对我来说,我需要指定不创建空间索引。所以你的模型将更改为:

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)

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)
新一帅帅 2024-10-07 02:22:09

一个快速的想法是有另一个布尔字段,当您有实际点时将其标记为真/假。这将使查询变得容易,因为您可以将布尔字段添加到 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文