无法在Laravel DigitalOcean管理的数据库中创建或更改没有主键的表

40

我刚刚使用(托管数据库)将我的应用部署到DigitalOcean,当我调用 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')

看起来当mysql变量sql_require_primary_key设置为true时,Laravel迁移不起作用。

您有任何解决方案吗?

14个回答

2
如果您正在导入到某个SQL客户端,请在导入之前在该特定数据库上运行此查询。 set sql_require_primary_key = off 对于DO托管的MySQL数据库,所有操作都很好。干杯!

0

-1
如果您在使用 Laravel 时看到此错误,最有可能是来自于 password_resets_table
请将您的迁移文件中的 up() 方法更新为以下内容:
  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');
  }

注意:记得导入 DB

use Illuminate\Support\Facades\DB;


-3
将此行添加到您的迁移文件中。 $table->increments('aid');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接