Unable to create or change a table without a primary key - Laravel DigitalOcean Managed Database
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
I've just deployed my app to DigitalOcean using (Managed Database) and I'm getting the following error when calling php artisan migrate
php
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?
Introduction: The MySQL system variable 'sql_require_primary_key' affects how Laravel Migrations interact with the database. When this variable is set to true, it requires all tables to have a primary key; otherwise, an error occurs during migration. However, there are times when you may not need or want to have a primary key for every table in your application. In these situations, you can follow these steps for working around the issue with DigitalOcean Managed Databases and Laravel Migrations. Solution 1: Disable sql_require_primary_key Option To overcome this error, you may choose to disable the 'sql_require_primary_key' option temporarily. You can do that by setting it to false in your MySQL configuration file or through an environment variable. Here is a sample code snippet:php
# Include the current configuration
include_once '/etc/mysql/my.cnf';
# Set sql_require_primary_key to false
$config['mysqld'] = array_merge($config['mysqld'], [
'sql-require-primary-key' => 0,
]);
# Save the updated configuration
file_put_contents('/etc/mysql/my.cnf', ini_set('auto_prepend_file', '/dev/null') . PHP_EOL . "<?php" . 'include_once '/etc/mysql/my.cnf'; print($config); ?>' . PHP_EOL, FILE_APPEND | LOCK_EX);php
DB::table('users')->addColumn('id', 'integer', array('auto_increment' => true));