PHPackages                             chrisnharvey/laravel-sql-require-primary-key - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. chrisnharvey/laravel-sql-require-primary-key

ActiveLibrary[Database &amp; ORM](/categories/database)

chrisnharvey/laravel-sql-require-primary-key
============================================

519.7k↓39.4%2PHP

Since Mar 16Pushed 5y ago1 watchersCompare

[ Source](https://github.com/chrisnharvey/laravel-sql-require-primary-key)[ Packagist](https://packagist.org/packages/chrisnharvey/laravel-sql-require-primary-key)[ RSS](/packages/chrisnharvey-laravel-sql-require-primary-key/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel sql\_require\_primary\_key workaround
=============================================

[](#laravel-sql_require_primary_key-workaround)

This package was created to work around an issue when using Laravel's schema builder with a MySQL server that enforces primary keys (DigitalOcean Managed Databases for example).

The problem
-----------

[](#the-problem)

This issue occurs because the `sql_require_primary_key` flag in MySQL prevents the creation of tables without a primary key. Because of the way Laravel's Schema builder assigns primary keys to non-serial columns, he schema builder will first create the table without a primary key, then immediately alter the table to assign a primary key.

### Example

[](#example)

```
Schema::create('my_table', function (Blueprint $table) {
    $table->string('id')->primary();
});
```

The above call to the schema builder will create the below queries:

```
create table `my_table` (`id` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
alter table `my_table` add primary key `my_table_id_primary`(`id`);
```

If your MySQL server has `sql_require_primary_key` on, you will receive an error like this:

```
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.

```

The solution
------------

[](#the-solution)

Installing this package will prevent this issue from occuring while still ensuring that primary keys have been set on every table.

It works by listening for Laravel's migration events, and parsing the queries that are going to be run to check if primary keys have been assigned to each table, and will throw an exception if it finds a missing primary key.

It then temporarily disables `sql_require_primary_key`, runs your migration, then restores `sql_require_primary_key` to its original value.

Installation
------------

[](#installation)

This package can be installed via composer:

```
composer require chrisnharvey/laravel-sql-require-primary-key

```

If you have automatic package discovery enabled, then you're done. Otherwise, you will need to register the following service provider:

```
ChrisHavey\LaravelSqlRequirePrimaryKey\ServiceProvider::class
```

Usage
-----

[](#usage)

Add the following config key to your MySQL database config.

```
'require_primary_key' => true
```

Disabling checks for a single migration
---------------------------------------

[](#disabling-checks-for-a-single-migration)

In most cases, you will not need this, but sometimes (particularly with raw queries) this package may not detect primary keys and throw an exception. To disable these checks, add the following property to the migration:

```
public $skipPrimaryKeyChecks = true;
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/619298?v=4)[Chris Harvey](/maintainers/chrisnharvey)[@chrisnharvey](https://github.com/chrisnharvey)

---

Top Contributors

[![chrisnharvey](https://avatars.githubusercontent.com/u/619298?v=4)](https://github.com/chrisnharvey "chrisnharvey (12 commits)")

### Embed Badge

![Health badge](/badges/chrisnharvey-laravel-sql-require-primary-key/health.svg)

```
[![Health](https://phpackages.com/badges/chrisnharvey-laravel-sql-require-primary-key/health.svg)](https://phpackages.com/packages/chrisnharvey-laravel-sql-require-primary-key)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
