PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
我正在使用 laravel 6.2,连接是 SQL。我正在创建两个 tables,'one to many relationship.' Table 'users' 和 'managers',每个用户将有一个经理,每个经理将有多个一个用户。
下面是用户table迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->integer('totalBalance')->default(21);
$table->integer('annualBalance')->default(15);
$table->integer('casualBalance')->default(6);
$table->timestamps();
});
Schema::table('users', function (Blueprint $table) {
$table->bigInteger('manager_id')->unsigned()->index();
$table->foreign('manager_id')->references('id')->on('managers')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
下面是管理器迁移 table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateManagersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('managers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('managers');
}
}
下面是用户模型:
<?php
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
// Rest omitted for brevity
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
public function setPasswordAttribute($value) {
$this->attributes['password'] = bcrypt($value);
}
public function manager()
{
return $this->belongsTo('App\Manager');
}
}
以下是经理模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Manager extends Model
{
protected $primaryKey = 'id';
public function users()
{
return $this->hasMany('App\User');
}
}
我收到以下错误:
我已经尝试了很多东西,我从网上的其他问题中看到的,包括更改id的类型(从BigInteger和integer,以及将database.php中的engine更改为''InnoDB',并将用户模型分成两部分(第二部分用于添加外键)。
我在网上看到一个东西(但没弄清楚如何实现),就是改变时间戳的顺序,因为有人说这个错误可能与此有关。
有什么建议吗?
发生这种情况是因为用户迁移 运行 在创建管理员的 table 之前。所以使用以下内容更改迁移将帮助您在管理器 table.
下创建外键
// user migration file
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->integer('totalBalance')->default(21);
$table->integer('annualBalance')->default(15);
$table->integer('casualBalance')->default(6);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
// managers migration file
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateManagersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('managers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function (Blueprint $table) {
$table->bigInteger('manager_id')->unsigned()->index();
$table->foreign('manager_id')->references('id')->on('managers')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('managers');
}
}
我正在使用 laravel 6.2,连接是 SQL。我正在创建两个 tables,'one to many relationship.' Table 'users' 和 'managers',每个用户将有一个经理,每个经理将有多个一个用户。
下面是用户table迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->integer('totalBalance')->default(21);
$table->integer('annualBalance')->default(15);
$table->integer('casualBalance')->default(6);
$table->timestamps();
});
Schema::table('users', function (Blueprint $table) {
$table->bigInteger('manager_id')->unsigned()->index();
$table->foreign('manager_id')->references('id')->on('managers')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
下面是管理器迁移 table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateManagersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('managers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('managers');
}
}
下面是用户模型:
<?php
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
// Rest omitted for brevity
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
public function setPasswordAttribute($value) {
$this->attributes['password'] = bcrypt($value);
}
public function manager()
{
return $this->belongsTo('App\Manager');
}
}
以下是经理模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Manager extends Model
{
protected $primaryKey = 'id';
public function users()
{
return $this->hasMany('App\User');
}
}
我收到以下错误:
我已经尝试了很多东西,我从网上的其他问题中看到的,包括更改id的类型(从BigInteger和integer,以及将database.php中的engine更改为''InnoDB',并将用户模型分成两部分(第二部分用于添加外键)。
我在网上看到一个东西(但没弄清楚如何实现),就是改变时间戳的顺序,因为有人说这个错误可能与此有关。
有什么建议吗?
发生这种情况是因为用户迁移 运行 在创建管理员的 table 之前。所以使用以下内容更改迁移将帮助您在管理器 table.
下创建外键// user migration file
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->integer('totalBalance')->default(21);
$table->integer('annualBalance')->default(15);
$table->integer('casualBalance')->default(6);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
// managers migration file
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateManagersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('managers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function (Blueprint $table) {
$table->bigInteger('manager_id')->unsigned()->index();
$table->foreign('manager_id')->references('id')->on('managers')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('managers');
}
}