【问题标题】:Laravel - Its Possible Add Foreign Key On Table With Data Type String With Unsigned?Laravel - 它可能在具有无符号数据类型字符串的表上添加外键?
【发布时间】:2019-02-07 12:26:27
【问题描述】:

我有两张桌子,table universitas 和 table fakultas

关于模式表大学,如下所示

public function up()
    {
        Schema::create('universitas', function (Blueprint $table) {
            $table->string('id');
            $table->string('nama_universitas');
            $table->timestamps();
        });
    }

和如下表 fakultas

public function up()
    {
        Schema::create('fakultas', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nama_fakultas');
            $table->string('nama_universitas')->unsigned();
            $table->foreign('nama_universitas')->references('id')->on('universitas');
            $table->string('keterangan');
            $table->timestamps();
        });
    }

如果我使用数据类型字符串并使用无符号,这可能吗?

当我使用 php artisan migrate 执行时,我得到一个错误

【问题讨论】:

    标签: php laravel migration


    【解决方案1】:

    从数据库的角度来看,在字符串上使用 unsigned 是没有意义的。 unsigned 的目的是判断一个数值字段是否可以取负值。例如,一个:

    $table->tinyInteger('foo');
    

    将创建一个取值介于 -128 和 +128 之间的数据库字段,而

    $table->tinyInteger('foo')->unsigned();
    

    将创建一个范围为 0 - 255 的数据库字段

    外键的重要一点是外键的数据类型必须匹配另一个表的主键的数据类型,无论是数字还是字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2016-11-21
      • 2020-05-25
      相关资源
      最近更新 更多