Php Laravel-是否可以在数据类型为字符串且无符号的表上添加外键?
我有两张桌子,universitas桌子和fakultas桌子 在模式表universita上,如下所示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
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的数据库字段
外键的重要一点是,外键的数据类型必须与另一个表的主键的数据类型相匹配,无论是数字还是字符串