I am very confused now, because I cannot get this to work. I have a tbl_address which already has longitude and latitude. I want to add a spatial index for my search. So I want to add a location POINT column in my database. I tried this one:
alter table tbl_address
add column location POINT GENERATED ALWAYS AS (ST_SRID(POINT(longitude, latitude), 4326)) STORED NOT NULL;
But when I then try to add my SI, I receive this warning back:
CREATE SPATIAL INDEX idx_location ON tbl_address (location);
0 row(s) affected, 1 warning(s): 3674 The spatial index on column 'location' will not be used by the query optimizer since the column does not have an SRID attribute. Consider adding an SRID attribute to the column. Records: 0 Duplicates: 0 Warnings: 1
I get that just my entries do have the 4326 srid, but if I try to add it to the column itself (POINT SRID 4326) I am receiving a syntax error on GENERATED ALWAYS AS.
How can I make it work? It would be great to have location updated when longitude/latitude changes. Do I really need to implement a trigger? I am using MySQL 8.
Thanks!
Edit: My mysql version is 8.0.43-0ubuntu0.22.04.1 and this is the test table I am using:
CREATE TABLE IF NOT EXISTS tbl_address
(
address_id int not null auto_increment,
latitude DECIMAL(9,2) NOT NULL default 0,
longitude DECIMAL(9,2) NOT NULL default 0,
location POINT not null SRID 4326,
Primary Key (address_id)
);
Not sure why it is working for you guys, maybe it is an issue with this specific version?
SELECT VERSION();Also show the complete error message for the syntax error you got.DECIMAL(9,2)for the longitude and latitude columns, but this was a guess. It helps people find the right answer if you show your reproducible test case.st_geominstead of apointallow you to build your index, as explictly told in this comment (found through a less interesting one).