views:

437

answers:

2

Hi,

I am trying to add a spatial index to a table column named Location of type BLOB. If I try this:

ALTER TABLE route ADD SPATIAL INDEX(Location);

I get:

Error: BLOB/TEXT column 'Location' used in key specification without a key length

But in the official docs for MySql 5.1 (the version I am using), it clearly says when referring to spatial indexes:

"In MySQL 5.1, column prefix lengths are prohibited. The full width of each column is indexed."

This surely says that I don't need to provide a prefix. I tried adding a prefix anyway like this:

ALTER TABLE route ADD SPATIAL INDEX(Location(256));

And I get:

Error: Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys

So what the heck is going on?? For info, I am using MySQL 5.1.37 community, and my table is MyISAM, this is the create statement:

CREATE TABLE `climb`.`route` ( 
`Id` int(11) NOT NULL, 
`Name` varchar(255) NOT NULL, 
`Location` blob, 
PRIMARY KEY (`Id`), 
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

PS I have also tried making Location NOT NULL, this made no difference.

A: 

I believe the column you try to add spatial index to should be declared not null.

Zed
Thanks for the idea, but unfortunatley this has made no difference :( I have made Location NOT NULL (have updated my post to show this), and still get the same error when I try to apply a spatial index.
James Allen
A: 

Spatial indexes should be created on GEOMETRY types.

CREATE TABLE `route` (
        `Id` int(11) NOT NULL, 
        `Name` varchar(255) NOT NULL, 
        `Location` GEOMETRY NOT NULL,
        PRIMARY KEY (`Id`),
        SPATIAL KEY (`Location`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Quassnoi
Correct, although MySql supports using BLOBs for geometry columns (http://dev.mysql.com/doc/refman/5.1/en/gis-wkb-format.html) in the Well-Known Binary (WKB) Format, it seems you can't have a spatial index. The trick was getting Fluent NHibernate to do this, I had to force the SQL type with .CustomSqlTypeIs("GEOMETRY"). Thanks!
James Allen