Php 不能与migration Laravel同时生成主键和外键
我试图在一次迁移时在主键中生成外键 这是给拉威尔5.7的。我尝试了不同的方法来实现我的目标。这是我的最终代码Php 不能与migration Laravel同时生成主键和外键,php,laravel,laravel-5.7,Php,Laravel,Laravel 5.7,我试图在一次迁移时在主键中生成外键 这是给拉威尔5.7的。我尝试了不同的方法来实现我的目标。这是我的最终代码 Schema::create('teachers', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->integer('user_id')->unsigned()->primary(); $table->timestamp
Schema::create('teachers', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('user_id')->unsigned()->primary();
$table->timestamps();
});
Schema::table('teachers', function($table) {
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
它生成一个普通的表,主键为“user\u id”,但不是外键。试试这段代码
Schema::create('teachers', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
你试过这个吗
Schema::create('teachers',函数(Blueprint$table){
$table->engine='InnoDB';
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->primary('user_id');
$table->foreign('user\u id')
->引用('id')
->关于('用户')
->onDelete(“级联”);
});
我不认为
primary()!我不知道这两个链接是否有帮助:
Schema::create('teachers', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('user_id')->unsigned()->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTeachersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasTable('teachers'))
{
Schema::create('teachers', function (Blueprint $table) {
$table->increments('id');
$table->string('teacher_name');
$table->unsignedInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('teachers');
}
}