【发布时间】:2016-03-11 02:17:28
【问题描述】:
我需要为我的数据库使用外键,但我不能这样做,在命令行中运行迁移命令后,我收到此错误:
[Illuminate\Database\QueryException] SQLSTATE[HY000]:一般错误: 1215 无法添加外键约束(SQL:alter table
samplesadd 约束 amples_supplier_id_foreign 外键 (supplier_id) 参考suppliers(id))[PDOException] SQLSTATE[HY000]:一般错误:1215 无法添加 外键约束
样本迁移:
Schema::create('samples', function (Blueprint $table) {
$table->Increments('id',true);
$table->string('variety',50);
$table->integer('supplier_id')->unsigned();
$table->foreign('supplier_id')->references('id')->on('suppliers');
$table->string('lot_number');
$table->date('date');
$table->integer('amount');
$table->integer('unit_id')->unsigned();
$table->foreign('unit_id')->references('id')->on('unit');
$table->string('technical_fact');
$table->string('comments');
$table->string('file_address');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('category');
$table->timestamps();
});
供应商迁移:
Schema::create('suppliers', function (Blueprint $table) {
$table->Increments('id',true);
$table->string('supplier',50);
$table->timestamps();
});
我尝试通过新的样本迁移来实现这一点,但没有成功:
Schema::create('samples', function (Blueprint $table) {
$table->Increments('id',true);
$table->string('variety',50);
$table->integer('supplier_id')->unsigned();
$table->string('lot_number');
$table->date('date');
$table->integer('amount');
$table->integer('unit_id')->unsigned();
$table->string('technical_fact');
$table->string('comments');
$table->string('file_address');
$table->integer('category_id')->unsigned();
$table->timestamps();
});
Schema::table('samples', function($table) {
$table->foreign('supplier_id')->references('id')->on('suppliers');
$table->foreign('unit_id')->references('id')->on('unit');
$table->foreign('category_id')->references('id')->on('category');
});
我尝试将主键的长度固定为10,但再次失败
【问题讨论】:
标签: php mysql laravel migration laravel-5.1