Laravel 13.20 blocks database errors triggered by rapid AI code generation
The Problem with Same-Second Migrations

When working with AI coding assistants or automated scripts, speed can occasionally break your local development environment. When an AI agent rapidly executes multiple commands to scaffold resources, it can generate multiple Laravel database migrations within the exact same second.
Because Laravel uses a timestamp-based prefix (YYYY_MM_DD_HHMMSS) to determine the execution order of migrations, same-second migrations default to alphabetical sorting. This behavior creates a serious issue when a child table (like client_contacts) sorts alphabetically before its parent table (clients).
SQLite vs MySQL Silent Failures
This sorting issue behaves differently depending on your local database engine. If you are developing with SQLite, the database schema runner does not immediately enforce foreign key constraints during creation. The migrations run out of order without throwing an error, leaving developers unaware of the underlying issue.
Once the project is deployed to a MySQL or PostgreSQL database, the migration runner immediately throws a "failed to open table" error because the parent table does not exist yet.
How Laravel 13.20 Patches the Bug
To resolve this race condition, a new pull request merged into Laravel changes how the framework generates migration filenames. Instead of blindly writing the current timestamp, Laravel now checks the target directory for pre-existing timestamp prefixes.
Initially, developer Push Back proposed checking the migration directory and adding one second iteratively until finding a free prefix. Subsequently, developer Nick refined this logic to introduce collisionFreePath, resolving edge cases and maintaining backward compatibility.
// The framework now resolves collisions by incrementing the timestamp prefix
protected function collisionFreePath($name, $path)
{
$timestamp = date('Y_m_d_His');
while ($this->files->exists($path = $this->getPath($name, $path, $timestamp))) {
$timestamp = $this->incrementTimestamp($timestamp);
}
return $path;
}
Verification and Testing
To test this fix, run a batch of model creation commands sequentially in a single terminal execution:
php artisan make:model Task -m && php artisan make:model TaskSubtask -m
Even though both migrations execute within milliseconds of each other, Laravel automatically increments the second of the latter file (e.g., sequentially naming them 06, 07, and 08), ensuring they execute in the correct logical order.

Important Fix in Laravel 13.20: Same-Second Migrations
WatchLaravel Daily // 5:36