我想迁移包含三个表的数据库:
Ad_users
广告组
Ad_usersxad_groups
后者显然是一个联结表,仅包含两个引用其他两个表的PK的FK。现在,我遇到以下问题,这是ad_users表的迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAdusersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('Ad_users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('common_name');
$table->string('location');
$table->string('description');
$table->integer('postalcode');
$table->string('physical_delivery_office_name');
$table->string('telephone_number');
$table->string('initials');
$table->string('street_address');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('Ad_users');
}
}
$table->bigIncrements('id');
创建一个类型为bigint(20) unsigned
的列。当然,联结表中包含FK的列必须完全相同。但是,外键当然不能是联结表的主键,因此我不能使用$table->bigIncrements
语法,但是instad必须使用$table->bigInteger
语法,如在Ad_usersxad_groups表的迁移中所示:] >
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateAdusersxadgroupsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('Ad_usersxad_groups', function (Blueprint $table) { $table->bigInteger('Ad_user_id'); $table->bigInteger('Ad_group_id'); $table->foreign('Ad_user_id')->references('id')->on('Ad_users'); $table->foreign('Ad_group_id')->references('id')->on('Ad_groups'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('Ad_usersxad_groups'); } }
出现的问题:
$table->bigInteger
创建一个类型为bigint(20)
的列。看来这与在原始表的PK列内键入bigint(20) unsigned
。当我尝试运行迁移时,Artisan引发以下错误:
SQLSTATE[HY000]: General error: 1005 Can't create table `aetherdb`.`ad_usersxad_groups` (errno: 150 "Foreign key constraint is incorrectly fo rmed") (SQL: alter table `Ad_usersxad_groups` add constraint `ad_usersxad_groups_ad_user_id_foreign` foreign key (`Ad_user_id`) references `A d_users` (`id`))
除了摆脱原始表的PK列上的
bigint(20) unsigned
类型之外,还有什么方法可以解决此问题?我可以以某种方式将unsigned
添加到联结表的非主键列中吗?
我想迁移一个包含三个表的数据库:Ad_users Ad_groups Ad_usersxad_groups显然,后者是一个联结表,仅包含两个引用其他两个表PK的FK。 ...
您可以使用$table->bigInteger('Ad_user_id')->unsigned();
将此FK取消签名。