fix: #1286 duplicate key value on Pgsql

This commit is contained in:
Phan An 2021-05-21 14:24:20 +02:00
parent 6b7cb88e6d
commit 27b97fa317
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
2 changed files with 24 additions and 0 deletions

View file

@ -5,6 +5,7 @@ namespace Database\Seeders;
use App\Models\Album;
use App\Models\Artist;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class AlbumTableSeeder extends Seeder
{
@ -17,5 +18,16 @@ class AlbumTableSeeder extends Seeder
'name' => Album::UNKNOWN_NAME,
'cover' => Album::UNKNOWN_COVER,
]);
self::maybeResetPgsqlSerialValue();
}
private static function maybeResetPgsqlSerialValue(): void
{
if (DB::getDriverName() === 'pgsql') {
DB::statement(
"SELECT setval(pg_get_serial_sequence('albums', 'id'), coalesce(max(id), 0) + 1, false) FROM albums"
);
}
}
}

View file

@ -4,6 +4,7 @@ namespace Database\Seeders;
use App\Models\Artist;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class ArtistTableSeeder extends Seeder
{
@ -14,5 +15,16 @@ class ArtistTableSeeder extends Seeder
], [
'name' => Artist::UNKNOWN_NAME,
]);
self::maybeResetPgsqlSerialValue();
}
private static function maybeResetPgsqlSerialValue(): void
{
if (DB::getDriverName() === 'pgsql') {
DB::statement(
"SELECT setval(pg_get_serial_sequence('artists', 'id'), coalesce(max(id), 0) + 1, false) FROM artists"
);
}
}
}