How to Fix MySQL Error 3904 (ER_JSON_VALUE_OUT_OF_RANGE_FOR_FUNC_INDEX)
You get mysql error 3904 when a JSON value exceeds the size limit of a functional index cast. You can fix it by widening the cast data type or cleaning the JSON input data. Database teams use functional indexes to speed up queries on JSON documents. The database engine extracts values from JSON columns and converts them into typed index entries. When an incoming value is too large or too small for that target type, the write fails. What causes mysql error 3904 in the database engine MySQL tr
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 3904 when a JSON value exceeds the size limit of a functional index cast. You can fix it by widening the cast data type or cleaning the JSON input data.
Database teams use functional indexes to speed up queries on JSON documents. The database engine extracts values from JSON columns and converts them into typed index entries. When an incoming value is too large or too small for that target type, the write fails.
What causes mysql error 3904 in the database engine
MySQL tracks this failure under symbol ER_JSON_VALUE_OUT_OF_RANGE_FOR_FUNC_INDEX and SQLSTATE 22003. The engine prints a clear message stating that the JSON value is out of range for the cast. It also names the exact functional index that rejected the write operation.
Functional indexes on JSON columns rely on type casting to build index trees. Multi-valued indexes and scalar functional indexes both cast JSON elements into standard SQL data types. For example, an index might cast a JSON array of numbers into unsigned integers.
If an app sends a negative number into an unsigned index cast, the engine stops the query. The same problem happens when a large integer exceeds the maximum limit of a smaller integer type. String casts can also fail if character lengths or numeric decimals exceed the defined bounds.
This error occurs during insert, update, or alter table operations. When you add a new functional index to an existing table, the engine scans all rows. One bad JSON document in millions of rows will cause the index creation to fail.
How to find the bad JSON data in your tables
First, read the error message to find the exact name of the failing functional index. Next, check the table definition to see which JSON path and target data type that index uses. This tells you the exact minimum and maximum bounds that the engine expects.
If the error happens during an app write, inspect the payload that triggered the failure. Look at the specific JSON field mapped to the index path. You will often find a negative number, a huge timestamp, or an unexpected string value.
DPoP (Demonstration of Proof-of-Possession) + JSON Web Tokens (JWT): architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If the error happens while creating a new index, query the existing table to find outlier rows. Filter the JSON path for values that fall outside the bounds of your target cast type. Also check for wrong JSON types, such as nested objects where the index expects single numbers.
When running diagnostic queries in production, protect sensitive fields stored inside the same JSON column. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in query results. This lets engineers inspect broken JSON structures safely while keeping personal data private.
How to fix out of range functional index casts
You have two main ways to fix this error safely in a live database. Your choice depends on whether the out-of-range JSON value is valid business data or a bug.
If the large value is valid, you must widen the target type in the functional index. Drop the old functional index and create a new one with a larger cast type. For instance, switch from a standard integer cast to a big integer or signed integer cast.
If the value is a bug, fix the data before it reaches the index. Update the broken JSON rows in your table so every value fits the target type. Then add validation rules in your app or database constraints to block bad inputs early.
Do not disable strict SQL modes to bypass this range check in production. Turning off strict checks can cause silent data truncation and corrupt your index results. Always match your index definition to the true range of your JSON data.
Check MySQL queries and results on the wire
MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
What does SQLSTATE 22003 mean in MySQL?
SQLSTATE 22003 is the standard code for a numeric value out of range error. In MySQL error 3904, it means a JSON value cannot fit into the cast type of an index.
Can a single bad row block a new functional index?
Yes, MySQL validates every existing row when you add a functional index to a table. If one row holds an out-of-range JSON value, the entire alter table command rolls back.
Does this error affect multi-valued JSON indexes?
Yes, multi-valued indexes use typed array casts to index multiple elements inside a JSON array. If any single element in that array falls outside the target type bounds, MySQL throws error 3904.
Related guides
In this post