chrisnharvey/laravel-sql-require-primary-key
Composer 安装命令:
composer require chrisnharvey/laravel-sql-require-primary-key
包简介
无描述信息
README 文档
README
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
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
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
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
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
Add the following config key to your MySQL database config.
'require_primary_key' => true 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;
chrisnharvey/laravel-sql-require-primary-key 适用场景与选型建议
chrisnharvey/laravel-sql-require-primary-key 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.58k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 chrisnharvey/laravel-sql-require-primary-key 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chrisnharvey/laravel-sql-require-primary-key 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 17.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04