Skip to content
PHP, Laravel & WordPress #Laravel #MySQL #PHP

SQLSTATE[22001] 1406 Data Too Long for Column: How to Fix It in Laravel and MySQL

"String data, right truncated: 1406 Data too long for column" means a value is longer than its column. Why it suddenly appears, how to widen the column safely in Laravel, and why you shouldn't turn strict mode off.

Updated

A form that worked for months suddenly fails for one user, or an import stops halfway, and the log shows:

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'title' at row 1
(Connection: mysql, SQL: insert into `posts` (`title`, ...) values (...))

The message is accurate: the value is longer than the column allows. The useful questions are why it started now, which fix is right, and how to stop it reaching the database in the first place.

What the error means

Every text column has a maximum size. VARCHAR(255) holds up to 255 characters, TEXT up to 65,535 bytes. When an INSERT or UPDATE tries to store something longer, MySQL has two options:

  • Strict mode on (the default since MySQL 5.7, including MySQL 8.4): reject the statement with error 1406 and roll it back.
  • Strict mode off: cut the value to fit, store it, and issue only a warning.

Laravel turns strict mode on for MySQL connections ('strict' => true in config/database.php), whatever the server's own setting. So in a Laravel app you get the error instead of quietly losing the end of the text, which is what you want.

The 22001 part is the standard SQL error class for "string data, right truncated". PostgreSQL uses the same code with its own wording: value too long for type character varying(255).

Why it suddenly appears

  • A user typed more than before. Laravel's $table->string('title') creates VARCHAR(255). A long title, a URL with tracking parameters, or a pasted paragraph goes past it.
  • The data comes from somewhere else. An API, a CSV import or a webhook sends longer values than your test data had. Long URLs, user agents and OAuth tokens are the usual culprits.
  • You moved servers. The old database had strict mode off and was silently truncating. The new one, or the switch to Laravel's default config, has it on. The data was too long all along; you just weren't told.
  • Something is encoded twice. HTML entities or JSON escaping can make a 200-character string 300 characters long.

Step 1: find the column and how big it is

The error names the column. Check how it's defined:

SHOW CREATE TABLE posts\G

Then find the longest values already stored, which tells you how much room you really need:

SELECT MAX(CHAR_LENGTH(title)) FROM posts;

If you have the value that failed (from the log or the request), measure it too. CHAR_LENGTH counts characters; LENGTH counts bytes. VARCHAR limits are in characters, while TEXT limits are in bytes, and with utf8mb4 one character can take up to 4 bytes.

Step 2: choose the fix

The data is legitimately longer: make the column bigger

Common right sizes:

Kind of dataColumnLaravel
Names, titles, emailsVARCHAR(255)string('x')
URLs, user agentsVARCHAR(2048) or TEXTstring('x', 2048) or text('x')
Descriptions, comments, tokensTEXT (64 KB)text('x')
Article bodies, HTML, large JSONMEDIUMTEXT (16 MB)mediumText('x')

Change it with a new migration:

php artisan make:migration widen_posts_title
public function up(): void
{
    Schema::table('posts', function (Blueprint $table) {
        $table->string('title', 500)->change();
    });
}

Since Laravel 11, change() replaces the whole column definition. Any modifier you don't repeat is dropped. If the column was ->nullable(), had a ->default() or a comment, include those again in the change() line, or you'll lose them.

If the column is indexed, a much longer VARCHAR can hit MySQL's index size limit. A unique index on VARCHAR(1000) with utf8mb4, for example, fails with "max key length is 3072 bytes". In that case use TEXT without the index, or index a hash of the value instead.

On a big table, an ALTER TABLE that changes a column type can take a while and lock writes. Run it outside busy hours and take a backup first; our MySQL backups to R2 guide sets up a scheduled one.

The data shouldn't be that long: validate it

Most of the time a 255-character limit on a title is right, and the bug is that the app accepted 300 characters and passed them to the database. Validate in the request so the user gets a clear message:

$request->validate([
    'title' => ['required', 'string', 'max:255'],
    'url'   => ['nullable', 'url', 'max:2048'],
]);

Laravel's max rule counts characters for strings, matching how VARCHAR counts. Add maxlength="255" to the HTML input too, as a convenience, not as the only check.

The data comes from outside and can be cut: truncate on purpose

For data where the end doesn't matter, such as a user agent string or a third-party error message you only log, shorten it yourself before saving:

use Illuminate\Support\Str;

$log->user_agent = Str::limit($request->userAgent(), 500, '');

That keeps the decision visible in code, instead of relying on MySQL silently truncating.

What not to do

Don't turn strict mode off ('strict' => false) to make the error go away. The insert then succeeds and MySQL cuts the text without telling anyone. You trade one visible error for corrupt data you discover months later: half an address, a broken URL, a token that no longer works. Strict mode also catches other problems, such as invalid dates and division by zero.

Find the other columns before users do

Once one column has hit its limit, others are often close. This query lists VARCHAR columns in your database with their sizes, so you can compare them against MAX(CHAR_LENGTH(...)) for the ones that take user input:

SELECT TABLE_NAME, COLUMN_NAME, CHARACTER_MAXIMUM_LENGTH
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND DATA_TYPE = 'varchar'
ORDER BY TABLE_NAME, COLUMN_NAME;

FAQ

Why does it say "at row 1" when my table has thousands of rows?

"Row 1" refers to the rows in the failing statement, not the table. For a single insert it's always row 1. For a bulk insert it tells you which of the inserted rows was too long.

The same code works locally but fails in production. Why?

Usually the local database is SQLite, which doesn't enforce VARCHAR lengths, or a MySQL with strict mode off. Use the same database and settings locally that production uses, or at least run your tests against MySQL.

Does this happen with numbers too?

Numbers that don't fit give a different error: 1264 Out of range value for column. The fix is the same idea: a bigger type (for example BIGINT) or validation.

Bottom line

Error 1406 means a value is longer than its column, and strict mode is correctly refusing to cut it. Check the column size and the real data, then either widen the column with a migration (repeating every modifier in change()), validate input with max:, or truncate deliberately for data you only log. Don't switch strict mode off.

Behaviour checked on 2 November 2026 against the MySQL 8.4 reference manual (SQL modes and InnoDB limits) and the Laravel 13 documentation.

Comments 0

Be the first to comment.

Leave a comment