How to Fix MySQL Error 3728 (ER_SPATIAL_UNIQUE_INDEX)
You get mysql error 3728 when you try to make a spatial index unique or primary. To fix it, use a standard spatial index and add a separate primary key column. MySQL uses the symbol ER_SPATIAL_UNIQUE_INDEX and SQLSTATE HY000 for this failure. The engine states that spatial indexes cannot act as primary or unique indexes. This rule exists because spatial data uses bounding boxes rather than simple scalar values. When a migration script breaks this rule, the database rejects the table change righ
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You get mysql error 3728 when you try to make a spatial index unique or primary. To fix it, use a standard spatial index and add a separate primary key column.
MySQL uses the symbol ER_SPATIAL_UNIQUE_INDEX and SQLSTATE HY000 for this failure. The engine states that spatial indexes cannot act as primary or unique indexes. This rule exists because spatial data uses bounding boxes rather than simple scalar values. When a migration script breaks this rule, the database rejects the table change right away.
What causes mysql error 3728 in the database engine
MySQL stores spatial shapes like points, lines, and polygons using special geometry data types. To search these shapes fast, the engine builds a spatial index using an R-tree structure. Normal indexes use B-trees to sort text or numbers in a strict line. B-trees can check if two values are the exact same in a short step.
R-trees work in a different way. They group nearby shapes inside two-dimensional bounding boxes to speed up area searches. Because an R-tree indexes approximations of shapes, it cannot enforce strict uniqueness across rows. It also cannot serve as the primary row pointer for the storage engine.
This error often shows up when developers build location tables using an object-relational mapper. Many mapping tools try to mark a geometry column as a unique key by mistake. Other times, a data team imports a schema from another database engine with different rules. When MySQL reads that table definition, it stops the query and returns code 3728.
How to diagnose and fix the failure in production
To find the broken query, check your migration logs or database error output first. Look for any table creation or alteration steps that touch geometry, point, or polygon columns. You will see a unique or primary constraint attached to a spatial index definition.
Fixing the problem requires a clean separation of concerns in your table design. First, remove the unique or primary constraint from the spatial index definition. Define the index on your geometry column as a standard spatial index only. Make sure the spatial column still disallows null values, as the engine requires that rule.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Next, give the table a dedicated primary key using an integer or identifier column. If your app must prevent duplicate coordinates, enforce uniqueness on separate latitude and longitude columns. You can also compute a deterministic hash of the shape data in a regular column. Then you can place a standard unique B-tree index on that hash column instead.
Securing schema changes in production databases
When a release fails with code 3728, teams often rush to fix the live schema. Granting broad admin roles to developers during an incident creates a major security risk. Engineers might change the wrong table or read sensitive user rows by mistake.
Instead of sharing root credentials, route all database sessions through a central control plane. A team lead can review and approve the exact schema change before it runs. Also, data masking rewrites matched fields like emails, phone numbers, card numbers, or national IDs. This keeps private records safe while engineers check table structures and test their fixes.
Add wire guardrails and masking beside MySQL
Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.
With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.
Frequently asked questions
What does ER_SPATIAL_UNIQUE_INDEX mean in MySQL?
ER_SPATIAL_UNIQUE_INDEX is the official symbol for MySQL error code 3728 under SQLSTATE HY000. It means a query tried to make a spatial index into a primary or unique key.
How can I stop duplicate points if a spatial index cannot be unique?
Store a text hash of the geometry shape in a separate generated column. You can then place a standard unique index on that hash column to block duplicates.
Can a geometry column still have a regular unique index without the spatial keyword?
No, standard B-tree indexes do not support full spatial objects without prefix limits. Using a separate hash column or coordinate pair columns is the safest way to enforce uniqueness.
Related guides
In this post