Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ahoi's avatar
Level 5

Schema::replace does not change postgres data structure?

Hi there,

I would like to change the name of a table inside of a migration command I built:

        \Schema::rename('stored_events', 'stored_events_bak');
        \Schema::rename('stored_events_migrated', 'stored_events');

Now the problem is:

The stored_events_migrated table is indeed renamed to stored_events, but the Postgres data structure is still referencing the original table name:

1	id	int8	NO	NULL	"nextval('stored_events_migrated_id_seq'::regclass)"		NULL
0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're experiencing is because, when you rename a table in PostgreSQL using Laravel’s Schema::rename(), the table name itself is changed, but underlying dependencies like sequences (used for SERIAL/BIGSERIAL or identity-like columns) do not get renamed automatically. As a result, your new table (now stored_events) still references the original sequence: stored_events_migrated_id_seq.

Why?
Postgres sequences are independent database objects. When you create a table with an auto-incrementing primary key, Postgres creates a sequence e.g., stored_events_migrated_id_seq for stored_events_migrated, and the default value for the column references this sequence. Renaming the table does not rename its associated sequence or update the column's default.


Solution Steps

  1. Rename the Sequence
  2. Update the Table’s Default Reference

Example Migration

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

class RenameStoredEventsMigration extends Migration
{
    public function up()
    {
        // 1. Rename tables
        Schema::rename('stored_events', 'stored_events_bak');
        Schema::rename('stored_events_migrated', 'stored_events');

        // 2. Rename the sequence
        DB::statement('ALTER SEQUENCE stored_events_migrated_id_seq RENAME TO stored_events_id_seq');

        // 3. Update the default of the id column to use the new sequence
        DB::statement("ALTER TABLE stored_events ALTER COLUMN id SET DEFAULT nextval('stored_events_id_seq')");
    }

    public function down()
    {
        // To reverse (example; adjust as needed)
        DB::statement("ALTER TABLE stored_events ALTER COLUMN id SET DEFAULT nextval('stored_events_migrated_id_seq')");
        DB::statement('ALTER SEQUENCE stored_events_id_seq RENAME TO stored_events_migrated_id_seq');
        Schema::rename('stored_events', 'stored_events_migrated');
        Schema::rename('stored_events_bak', 'stored_events');
    }
}

Key Points:

  • Renaming tables does not affect sequence names or column default values.
  • Always manually handle sequence renames/references when renaming tables in PostgreSQL.

References:

Let me know if you need further clarification or help with this migration!

Tray2's avatar

I would do it in these steps.

  1. Create the bak table from a select CREATE TABLE stored_events_bak AS SELECT * FROM stored_events;
  2. Truncate the stored_events table.
  3. Make the needed changes to the stored_events table.
  4. Copy the data with the needed modifications back from the backup table.

Please or to participate in this conversation.