34

I've just deployed my app to DigitalOcean using (Managed Database) and I'm getting the following error when calling php artisan migrate

SQLSTATE[HY000]: General error: 3750 Unable to create or change a 
table without a primary key, when the system variable 'sql_require_primary_key'
is set. Add a primary key to the table or unset this variable to avoid
this message. Note that tables without a primary key can cause performance
problems in row-based replication, so please consult your DBA before changing
this setting. (SQL: create table `sessions` (`id` varchar(255) not null,
`user_id` bigint unsigned null, `ip_address` varchar(45) null,
`user_agent` text null, `payload` text not null, `last_activity` int not null)
default character set utf8mb4 collate 'utf8mb4_unicode_ci')

It appears that Laravel Migrations doesn't work when mysql var sql_require_primary_key is set to true.

Do you have any solutions for that?

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
Michal Deimert
  • 455
  • 1
  • 5
  • 8

13 Answers13

73

From March 2022, you can now configure your MYSQL and other database by making a request to digital ocean APIs. Here's the reference: https://docs.digitalocean.com/products/databases/mysql/#4-march-2022

STEPS TO FIX THE ISSUE:

Step - 1: Create AUTH token to access digital ocean APIs. https://cloud.digitalocean.com/account/api/tokens

STEP - 2: Get the database cluster id by hitting the GET request to the below URL with bearer token that you have just generated above.

URL: https://api.digitalocean.com/v2/databases

Step - 3: Hit the below URL with PATCH request along with the bearer token and payload.

URL: https://api.digitalocean.com/v2/databases/{YOUR_DATABASE_CLUSER_ID}/config

payload: {"config": { "sql_require_primary_key": false }}

That's all. It worked flawlessly.

For more information, please refer to API DOCS: https://docs.digitalocean.com/products/databases/mysql/#latest-updates

hamza ajaz
  • 978
  • 7
  • 6
36

I was trying to fix this problem with an import to DO Managed MySQL using a mysqldump file from a WordPress installation. I found adding this to the top of the file did work for my import.

SET @ORIG_SQL_REQUIRE_PRIMARY_KEY = @@SQL_REQUIRE_PRIMARY_KEY;
SET SQL_REQUIRE_PRIMARY_KEY = 0;

I then imported using JetBrains DataGrip and it worked without error.

Kevin Phillips
  • 461
  • 4
  • 2
  • 1
    Why not just add a primary key to the tables that were missing them before importing them? – darryn.ten May 05 '21 at 08:18
  • Note that according to DigitalOcean "MySQL databases containing tables without a primary key and which contain more than 5000 rows may experience replication issues." – Liam May 09 '22 at 18:03
  • If you're seeing this in 2022 or later, [this answer](https://stackoverflow.com/a/72438019/7437737) is what you're looking for as it doesn't require a manipulation of your mysqldump file. – Zach Gollwitzer Jun 22 '22 at 12:01
19

Add in your first migration:

\Illuminate\Support\Facades\DB::statement('SET SESSION sql_require_primary_key=0');

Inside: Schema::create() function.

Toni
  • 1,555
  • 4
  • 15
  • 23
Usman Ayub
  • 191
  • 1
  • 4
17

Just add set sql_require_primary_key = off Like this

Click to view image

to your SQL file.

Tejsingh
  • 277
  • 2
  • 4
  • 7
    set sql_require_primary_key =off; – Namal Aug 20 '20 at 02:03
  • 1
    This sis not allowed in DigitalOcean ManagedDatabases. – Peter Fox Feb 17 '21 at 10:33
  • Just FYI, you can set `sql_require_primary_key` off (set it to 0) on Digital Ocean managed databases. The default will always be on, but you can reset it for any session. Including a primary key on your table will always be easiest, otherwise the raw SQL to set this variable can be added to your laravel migration scripts for the tables that need it. – Jason Feb 16 '22 at 17:45
12

One neat solution is defined here. The solution is to add listeners to migrate scripts and turn sql_require_primary_key on and off before and after executing a migration. This solution solve the problem where one is unable modify migrations script such as when they are from a library or a framework like Voyager.

<?php

namespace App\Providers;

    use Illuminate\Database\Events\MigrationsStarted;
    use Illuminate\Database\Events\MigrationsEnded;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Event;
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider {
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register() {
    
            // check this one here https://github.com/laravel/framework/issues/33238#issuecomment-897063577
            Event::listen(MigrationsStarted::class, function (){
                if (config('databases.allow_disabled_pk')) {
                    DB::statement('SET SESSION sql_require_primary_key=0');
                }
            });
    
            Event::listen(MigrationsEnded::class, function (){
                if (config('databases.allow_disabled_pk')) {
                    DB::statement('SET SESSION sql_require_primary_key=1');
                }
            });
        } 
// rest of the class 

}
Yada
  • 30,349
  • 24
  • 103
  • 144
Adelin
  • 18,144
  • 26
  • 115
  • 175
6

For bigger sql file, can with this command (nano editor can open in 1 week if your file size is <8GB, lol):

First :

sed  -i '1i SET SQL_REQUIRE_PRIMARY_KEY = 0;' db.sql

Second :

sed  -i '1i SET @ORIG_SQL_REQUIRE_PRIMARY_KEY = @@SQL_REQUIRE_PRIMARY_KEY;' db.sql
idmahbub
  • 61
  • 1
  • 2
4

According to the MySQL documentation purpose of this system variable is

to avoid replication performance issues: "Enabling this variable helps avoid performance problems in row-based replication that can occur when tables have no primary key."

IMHO, there are two possible options to consider for your problem;

  • Add primary key to this and every table in your migration, including temporary tables. This one is better and i think more convenient way to do it since there is no drawback to have primary key for each table.

Whether statements that create new tables or alter the structure of existing tables enforce the requirement that tables have a primary key.

  • Change your provider because according to here "We support only MySQL v8."

Also here is the bug report

Ersoy
  • 8,816
  • 6
  • 34
  • 48
4

I contacted DigitalOcean via a ticket to ask if they want to disable the requirement and they did the next day :)

So you can just ask them

Thanks for getting in touch with us! I understand you will like to disable the primary requirement on your managed database. The primary requirement for your managed database ****** has been disabled

Ryon M
  • 81
  • 6
  • Yes but note that "MySQL databases containing tables without a primary key and which contain more than 5000 rows may experience replication issues." – Liam May 09 '22 at 18:01
3

Unfortunately, we can't change the sql_require_primary_key value in the digital ocean MySQL database. instead, you can set the id to the primary key just by adding primary()

MOUSUF C A
  • 31
  • 1
2

When enabled, sql_require_primary_key has these effects:

  1. Attempts to create a new table with no primary key fail with an error. This includes CREATE TABLE ... LIKE. It also includes CREATE TABLE ... SELECT, unless the CREATE TABLE part includes a primary key definition.
  2. Attempts to drop the primary key from an existing table fail with an error, with the exception that dropping the primary key and adding a primary key in the same ALTER TABLE statement is permitted.

Dropping the primary key fails even if the table also contains a UNIQUE NOT NULL index.

  1. Attempts to import a table with no primary key fail with an error.

Default value is OFF , but in your case you need to set OFF from ON

IMPORTANT LINK

HOW TO SET

Md. Amirozzaman
  • 1,083
  • 10
  • 21
1

If you're importing in some SQL client, just run this query on that particular database before importing.

set sql_require_primary_key = off

Works all good for DO managed Mysql Database. Cheers!

Usama Munir
  • 589
  • 9
  • 11
-1

If you see this error while using laravel, it's most likely from the password_resets_table

Update the up() method in your migration to below:

  public function up()
  {
    DB::statement('SET SESSION sql_require_primary_key=0');
    Schema::create('password_resets', function (Blueprint $table) {
      ....
    });
    DB::statement('SET SESSION sql_require_primary_key=1');
  }

Note: Remember to import DB

use Illuminate\Support\Facades\DB;

-3

add this line to your migration file. $table->increments('aid');