How to change sql_require_primary_key value permanently in MySQL 8 for Laravel?

Viewed 5393

I recently created a database server in Digital Ocean, and it only supports MySQL 8. When I try to import a database of my Laravel project it reports this error:

Unable to create or change a table without a primary key, when the system variable 'sql_require_primary_key' is set.

So I tried to change the sql_require_primary_key to OFF in mySQL server by running the command,

set sql_require_primary_key = off;

And it changed successfully, but after that it automatically returned to the previous setting.

In Laravel, some primary keys are set after creating the table, so it showing error while migrating. It's my live project so that I can not modify the migrations I already created.

Anyone knows how to change the sql_require_primary_key permanently on MySQL 8?

1 Answers

A temporary workaround is to add turn sql_require_primary_key off for a Laravel project, you may add a statement for each database connection.

Inside Illuminate/Session/Console/stubs/database.stub, add this above Schema::create():

use Illuminate\Support\Facades\DB;
DB::statement('SET SESSION sql_require_primary_key=0');
Schema::create('sessions' ...

Once you have done the migration and restore, you can remove this change.

If you use Digital Ocean's API, there documentation on the API here. Alternatively, you may contact their support to turn that requirement off for your server.

Related