TEXT type field error: Data truncation: Data too long

This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: text类型字段报错Data truncation: Data too long

| username: zzps

[TiDB Usage Environment] Production Environment
[TiDB Version] 8.1.0

[Encountered Problem: Phenomenon and Impact]
The table field is set to TEXT type, and when adding data, this field reports an error: Data truncation: Data too long
The string length of this field is: 62852
[Attachment: Screenshot/Log/Monitoring]

| username: zzps | Original post link

The method to print the length. It shouldn’t report an error in theory.

| username: 有猫万事足 | Original post link

Text string. The maximum column length is 65,535 bytes. The optional M parameter is in characters and is used to automatically select the most appropriate type for the TEXT column. For example, TEXT(60) will produce a TINYTEXT data type, which can hold up to 255 bytes, i.e., a 60-character UTF-8 string where each character can be up to 4 bytes (i.e., 4×60=240). The use of the M parameter is not recommended.

This document explains that the column length is in units of 65,535 bytes. However, if a character is in the default utf8_mb4 character set, one character is 4 bytes.

So your string has 62,852 characters, but the byte count may have already exceeded depending on the character set you have set.

| username: zzps | Original post link

You are right, I thought text meant a size of 6m.