I am using the Geospatial extension for MySQL. In the MyISAM table listings there is a GEOMETRY column p, and 2 float columns lat and lng. Values in p are set using UPDATE listings SET p = POINT(lat, lng);.
Problem: Now I want to set a spatial index on column p. When I tried:
ALTER TABLE listings ADD SPATIAL INDEX(p);
and also tried:
CREATE SPATIAL INDEX sp_index ON listings (p);
Both times, I get the error error : All parts of a SPATIAL index must be NOT NULL.
So (using Navicat) I tried making col p NOT NULL, but it requires a default value. What default value should I use for this? I tried using '' as the default value but it gave an error invalid default value for 'p'.
I tried setting the default value for p as POINT(0,0) but I get the error BLOB/TEXT column 'p' can't have a default value
How should I approach this problem to create the spatial index? Thanks!
Edit
After running query ALTER TABLE listings CHANGE p p POINT NOT NULL, running ADD SPATIAL INDEX(p) causes a strange error Lost connection to MySQL server during query. Other queries that does not involve creating an index works fine ie. SELECT * FROM listings
Results from SHOW CREATE TABLE listings
CREATE TABLE `listings` (
`listing_id` int(8) NOT NULL AUTO_INCREMENT,
`url` varchar(255) DEFAULT NULL,
`website_city` varchar(32) DEFAULT NULL,
`website` varchar(32) DEFAULT NULL,
`price` int(7) DEFAULT NULL,
`price_per_br` int(7) DEFAULT NULL,
`bedroom` int(2) DEFAULT NULL,
`bathroom` int(2) DEFAULT NULL,
`fee` varchar(10) DEFAULT NULL,
`address_1` varchar(255) DEFAULT NULL,
`address_2` varchar(255) DEFAULT NULL,
`city` varchar(64) DEFAULT NULL,
`state` varchar(32) DEFAULT NULL,
`postal` int(6) DEFAULT NULL,
`retrival_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`post_timestamp` timestamp NULL DEFAULT NULL,
`lat` float(10,6) DEFAULT NULL,
`lng` float(10,6) DEFAULT NULL,
`p` point NOT NULL,
`description` text,
`img_subpath` varchar(15) DEFAULT NULL,
`photos` text,
`reply_email` varchar(255) DEFAULT NULL,
`phone` varchar(16) DEFAULT NULL,
`has_dishwasher` tinyint(1) DEFAULT NULL,
`has_laundry` tinyint(1) DEFAULT NULL,
`has_fireplace` tinyint(1) DEFAULT NULL,
`has_elevator` tinyint(1) DEFAULT NULL,
`has_balcony` tinyint(1) DEFAULT NULL,
`has_deck` tinyint(1) DEFAULT NULL,
`has_backyard` tinyint(1) DEFAULT NULL,
`has_parking` tinyint(1) DEFAULT NULL,
`has_gym` tinyint(1) DEFAULT NULL,
`has_pool` tinyint(1) DEFAULT NULL,
`cats_ok` tinyint(1) DEFAULT NULL,
`dogs_ok` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`listing_id`),
KEY `lat lng` (`lat`,`lng`,`price`,`price_per_br`,`bedroom`,`bathroom`,`post_timestamp`) USING BTREE,
KEY `spatial` (`p`(25))
) ENGINE=MyISAM AUTO_INCREMENT=589605 DEFAULT CHARSET=latin1
I converted p to back to GEOMETRY, ran the query to add the index to p and got a new error Cannot get geometry object from data you send to the GEOMETRY field
This page InformIT: Data Types in MySQL claims that spatial columns cannot have a
DEFAULTvalue but I can’t locate that info in MySQL docs:Testing in my 5.1 box shows the same error as yours. So the claim is probably correct.