Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-是否可以在数据类型为字符串且无符号的表上添加外键?_Php_Laravel_Migration - Fatal编程技术网

Php Laravel-是否可以在数据类型为字符串且无符号的表上添加外键?

Php Laravel-是否可以在数据类型为字符串且无符号的表上添加外键?,php,laravel,migration,Php,Laravel,Migration,我有两张桌子,universitas桌子和fakultas桌子 在模式表universita上,如下所示 public function up() { Schema::create('universitas', function (Blueprint $table) { $table->string('id'); $table->string('nama_universitas'); $t

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

在模式表universita上,如下所示

public function up()
    {
        Schema::create('universitas', function (Blueprint $table) {
            $table->string('id');
            $table->string('nama_universitas');
            $table->timestamps();
        });
    }
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();
        });
    }
下面是fakultas表

public function up()
    {
        Schema::create('universitas', function (Blueprint $table) {
            $table->string('id');
            $table->string('nama_universitas');
            $table->timestamps();
        });
    }
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();
        });
    }
如果我使用数据类型string和unsigned,这是可能的吗

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

从数据库的角度来看,在字符串上使用unsigned没有任何意义。unsigned的目的是确定数字字段是否可以接受负值。例如,a:

$table->tinyInteger('foo');
将创建一个数据库字段,该字段的值介于-128和+128之间,而

$table->tinyInteger('foo')->unsigned();
将创建范围为0-255的数据库字段

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