From e17363c04c96bccb36409f523314c437d24d6941 Mon Sep 17 00:00:00 2001 From: Mahmoud Almontasser Date: Wed, 13 Nov 2024 12:34:46 +0200 Subject: [PATCH] Add numeric type check for 'id' column in PostgreSQL sequence fix --- database/seeders/DatabaseSeeder.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 633f6a91d..16beeec66 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -42,10 +42,15 @@ function fixPostgresSequence() $tables = \DB::select('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\' ORDER BY table_name;'); foreach ($tables as $table) { if (\Schema::hasColumn($table->table_name, 'id')) { - $seq = \DB::table($table->table_name)->max('id') + 1; - \DB::select('SELECT setval(pg_get_serial_sequence(\'' . $table->table_name . '\', \'id\'), coalesce(' . $seq . ',1), false) FROM ' . $table->table_name); + $columnType = \DB::select("SELECT data_type FROM information_schema.columns WHERE table_name = '{$table->table_name}' AND column_name = 'id'")[0]->data_type; + // Only proceed if the 'id' column is numeric + if (in_array($columnType, ['integer', 'bigint', 'smallint', 'smallserial', 'serial', 'bigserial'])) { + $seq = \DB::table($table->table_name)->max('id') + 1; + \DB::select('SELECT setval(pg_get_serial_sequence(\'' . $table->table_name . '\', \'id\'), coalesce(' . $seq . ',1), false) FROM ' . $table->table_name); + } } } } } } +