Snapey's avatar
Level 122

Migrations generated by AI

I used Claude to build a project from scratch.

When it created the migrations, it created them in batches, problem was, each batch got the same timestamp. As these tables were related, and had constraints, after I cloned the project and tried to migrate the database, it fell over a couple of times because of table dependencies in constraints.

Anyone encountered this and found a way to tell the agent to wait a second between file creation?

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! Here’s a helpful solution to the Laracasts question:


This is a common issue when generating Laravel migrations programmatically or with AI agents—migrations in the same batch may end up with identical timestamps, which can cause issues with the order of migration execution and thus with foreign key constraints.

Solution

The recommended way to handle this is to ensure each migration file has a unique timestamp in its filename. Laravel migration filenames should be in the format:

YYYY_MM_DD_HHMMSS_table_name.php

where HHMMSS is to the second. This orders the migrations when running php artisan migrate.

How to Fix

You have a few options:

1. Manually Adjust Timestamps

Rename the migration files so each has a unique timestamp, incrementing the seconds for each file. For example:

2024_06_12_100001_create_users_table.php
2024_06_12_100002_create_posts_table.php
2024_06_12_100003_create_comments_table.php

This will make sure Laravel picks up and runs them in the right order.

2. Use Laravel’s Make:Migration Command

Instead of using code generation by an AI, prefer running:

php artisan make:migration create_users_table

This will generate the migration file with the current timestamp.

3. Automate the Rename (Script)

If you have many files, you can use a quick script (PHP, Bash, Node.js, etc.) to append/increment the seconds in the filenames.

Example PHP Script:

$path = database_path('migrations');
$files = glob($path.'/*.php');
sort($files);

$timestamp = strtotime('2024-06-12 10:00:00');
foreach ($files as $file) {
    $newTimestamp = date('Y_m_d_His', $timestamp);
    $newName = preg_replace('/\d{4}_\d{2}_\d{2}_\d{6}/', $newTimestamp, basename($file));
    rename($file, $path.'/'.$newName);
    $timestamp++; // Increment seconds
}

4. When Using AI

If you’re using an agent like Claude to generate code, instruct it explicitly:

"Generate the migration files with at least a one second difference in the timestamp for each file."

If you’re using an API or script, add a sleep/delay of one second between file generations:

sleep(1); // Add after each file creation

Summing Up

  • Best practice: Always ensure unique timestamps for migration files that depend on each other.
  • Quick fix: Manually or automatically rename migration files.
  • Future: When using an agent, add a delay (e.g., sleep(1)) or ask it for unique filenames.

Let me know if you need a ready-to-use script to fix your current migrations!

Please or to participate in this conversation.