所以我在本地使用我的项目并运行此代码 php artisan migrate
一切正常,我的表将被创建
但是当我转到我的 vps 并做同样的事情时,我得到了这个错误
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter table
mediables
add constraintmediables_media_id_foreign
forei gn key (media_id
) referencesmedia
(id
) on delete cascade)
我遵循了这个网站的一些解决方案,但它们没有用
我的迁移文件:
Schema::create('media', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable();
$table->string('old_name')->nullable();
$table->text('desc')->nullable();
$table->string('category')->nullable();
$table->string('type');
$table->string('format');
$table->string('href');
$table->string('thumbnail')->nullable();
$table->timestamps();
});
Schema::create('mediables', function (Blueprint $table) {
$table->integer('media_id')->nullable();
$table->integer('mediable_id');
$table->string('mediable_type');
$table->primary(['media_id','mediable_id','mediable_type']);
$table->timestamps();
});
Schema::table('mediables', function (Blueprint $table) {
$table->foreign('media_id')
->references('id')->on('media')
->onDelete('cascade');
});
请注意,在我的项目中,媒体和其他模型之间存在多态关系
我的本地机器和我的 vps 之间的唯一区别是我在本地有 apache,如果相关的话,在 vps 上有 nginx
最佳答案
media
表应该有 id
列,因为 media_id
引用了它。
另外,如果id
是increment()
,你应该使用unsigned()
外键:
$table->integer('media_id')->unsigned()->nullable();
关于php - 无法在vps服务器上添加外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328384/