Skip to content

Instantly share code, notes, and snippets.

@ryantxr
Created March 25, 2024 01:04
Show Gist options
  • Save ryantxr/70c969a33c6cc23ca824b87f4a56e213 to your computer and use it in GitHub Desktop.
Save ryantxr/70c969a33c6cc23ca824b87f4a56e213 to your computer and use it in GitHub Desktop.
Laravel migration mod to turn off primary key requirement
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$originalSetting = DB::select("SHOW SESSION VARIABLES LIKE 'sql_require_primary_key'")[0]->Value;
// Turn off if necessary
if ($originalSetting == 'ON') {
DB::statement('SET SESSION sql_require_primary_key=0');
}
Schema::create('password_reset_tokens', function (Blueprint $table) {
$table->string('email')->primary();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
if ($originalSetting == 'ON') {
DB::statement('SET SESSION sql_require_primary_key=1');
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('password_reset_tokens');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment