SRID issues with GeoDjango on MySQL 8

10 views
Skip to first unread message

Utkarsh Bansal

unread,
May 25, 2020, 11:06:25 AM5/25/20
to Django users
GeoDjango is not respecting SRIDs with MySQL 8.


I created a new column

geo_code = PointField(srid=4326)


and ran makemigrations. Running sqlmigrate on the generated migration gives


BEGIN;
--
-- Create model Location
--
CREATE TABLE `locations_location` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `geo_code` POINT NOT NULL);
CREATE SPATIAL INDEX `locations_location_geo_code_id` ON `locations_location`(`geo_code`);
COMMIT;



See how the SRID was ignored.

Now anything I add to this column is using SRID 0 - which is incorrect. I get wrong results if I use the `ST_DISTANCE` function with SRID 0.

 
I tried using `migrations.SeparateDatabaseAndState` to create the column with the correct SRID constraint with the following SQL

ALTER TABLE backend_userprofile ADD COLUMN geocode_point POINT NULL SRID 4326;



Now if I try inserting data into this column I get the following error

django.db.utils.OperationalError: (3643, "The SRID of the geometry does not match the SRID of the
column 'geocode_point'. The SRID of the geometry is 0, but the SRID of the column is 4326.
Consider changing the SRID of the geometry or the SRID property of the column.")



Django is still trying to insert data with SRID 0 and MySQL raises and error because the column has a constrain on SRID.

Is there a way to make Django work with correct SRIDs?


Reply all
Reply to author
Forward
0 new messages