好吧,为什么我不能将空值保存到Point-,MultiPoint-或者PolygonField?
我创建了一个表单,用户可以在其中选择他们发现特定鸟类的区域或位置。要存储我使用的GeoDjango数据,可以将数据存储在PointField或PolygonField中。
然而,PointField并不是必需的,因此在表单中,我将required设置为False,这会给出以下错误;
作为记录,我使用MySQL作为数据库。我目前并不需要PostgreSQL附带的所有额外功能。
IntegrityError at /sighting/where-did-you-spot-the-bird/
(1048, "Column 'sighting' cannot be null")#models.py
from django.contrib.gis.db import models
class Sighting(models.Model)
name = models.CharField(max_length=140)
description = models.TextField()
sighting = models.PointField(
blank=True,
null=True,
spatial_index=False
)所以列sighting不能为null,为什么?我是不是遗漏了什么,或者总是需要一个值?
一种解决方案是保存一个默认值,假设Point(0,0),但他们应该是另一种解决方案。
任何想法都将不胜感激!
发布于 2021-09-14 15:00:04
MySQL不允许空的空间字段(已知陷阱:https://www.buzzphp.com/posts/geodjango-mysql-points-cant-be-null-what-other-empty-value-should-i-use)
添加空Point()的default或具有经度/经度0,0的点
class Sighting(models.Model)
sighting = models.PointField(
blank=True,
spatial_index=False,
default=Point(0, 0) # or Point([]) if allowed
)或者考虑使用PostgresSQL和PostGIS:当我们使用地理信息系统时,几乎根据定义,我们将使用大数据,而PostgreSQL更适合这个角色。(示例文章:https://www.enterprisedb.com/blog/postgresql-vs-mysql-360-degree-comparison-syntax-performance-scalability-and-features)
https://stackoverflow.com/questions/68919908
复制相似问题