konect/laravel-migration-compatibility

库,用于帮助迁移确定相关字段是整型还是大整型

1.9.0 2024-02-28 06:57 UTC

README

Tests Packagist Stable Version Packagist downloads StyleCI MIT Software License

Laravel 5.8 和 BigInt 问题

截至 Laravel 5.8,迁移模板默认在 ID 字段上使用 bigIncrements 方法。之前,ID 字段是使用 increments 方法创建的。

外键列必须是相同类型。因此,使用 increments 方法创建的列不能引用使用 bigIncrements 方法创建的列。

这个小小的变化是 的问题,这些包定义了对 默认 Laravel 用户表 的引用。

此包通过扩展 Laravel 的 Blueprint 类并添加一个可以检测实际引用字段类型的方法来解决此问题。

class CreateProfilesTable extends Migration
{
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('id');

            // Make `user_id` field the same type as the `id` field of the `user` table:
            $table->intOrBigIntBasedOnRelated('user_id', Schema::connection(null), 'users.id');

            //...

            $table->foreign('user_id')
                ->references('id')
                ->on('users');
        });
    }
//...

安装

composer require konekt/laravel-migration-compatibility

文档

有关详细用法和示例,请参阅文档或参阅此存储库中 docs/ 文件夹中的 markdown 文件。

有关更改列表,请参阅变更日志