php - 无法在vps服务器上添加外键约束

标签 php laravel

所以我在本地使用我的项目并运行此代码 php artisan migrate 一切正常,我的表将被创建

但是当我转到我的 vps 并做同样的事情时,我得到了这个错误

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter table mediables add constraint mediables_media_id_foreign forei gn key (media_id) references media (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 引用了它。

另外,如果idincrement(),你应该使用unsigned()外键:

$table->integer('media_id')->unsigned()->nullable();

关于php - 无法在vps服务器上添加外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328384/

相关文章:

php - 从 PHP 服务器获取图像和元数据到 android

php - PHP 和 MYSQL 中格式 DD/MM/YYYY 的字符串到日期的转换不起作用

php - 通过 id 在 mysql 表中获取要更新的文章?

php - 把 SELECT 变成 DELETE mysql php

php - Laravel 5 配置文件中的自定义配置文件访问

php - 如何在 Laravel 中使用级联删除?

php - 我从来没有使用 laravel 的经过身份验证的用户

php - 通过 mysql cli 快速连接 MySQL,但使用 PDO 速度慢

php - 如果抛出异常,Laravel 数据库事务会发生什么?

php - Spry 选项卡式面板不显示动态数据