Mysql (已解决)Laravel 6.2:迁移期间添加外键约束失败
我试图在Laravel中创建外键,但是当我使用artisan迁移表时,出现以下错误:Mysql (已解决)Laravel 6.2:迁移期间添加外键约束失败,mysql,laravel,eloquent,migration,laravel-6.2,Mysql,Laravel,Eloquent,Migration,Laravel 6.2,我试图在Laravel中创建外键,但是当我使用artisan迁移表时,出现以下错误: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint `users_ent_id_foreign` foreign key (`ent_id`) references `id_ent` (`entreprises`)) 我的“企业”迁移代码
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint `users_ent_id_foreign` foreign key (`ent_id`) references `id_ent` (`entreprises`))
我的“企业”迁移代码
public function up()
{
Schema::create('entreprises', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id_ent');
$table->string('name');
$table->string('numSiret', 14)->unique();
$table->integer('nbr_couvert')->length(10);
$table->timestamps();
});
}
“用户”的我的迁移代码
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id_user');
$table->string('log');
$table->string('name', 45);
$table->string('firstName', 45);
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->string('num_phone', 20);
$table->datetime('created_at');
$table->unsignedInteger('ent_id');
$table->integer('agenda_id')->nullable();
$table->tinyInteger('droitUser')->nullable();
$table->boolean('validateMail');
$table->string('key', 255);
});
Schema::table('users', function ($table) {
$table->foreign('ent_id')
->references('entreprises') //here
->on('id_ent'); //and here
});
我更改了迁移的顺序,表“Enterprises”在“users”表之前创建
我尝试了来自和的每一种解决方案,它们没有什么可以改变的
有什么建议吗
编辑解决方案
我真丢脸。。。当我引用我想引用的表格时,我犯了一个愚蠢的错误。。。现在一切都好了。。。对不起,您正在使用sqlite数据库吗?如果是,sqlite数据库默认禁用外键功能 默认情况下,SQLite禁用外键约束。使用SQLite时,在迁移中尝试创建外键之前,请确保在数据库配置中启用外键支持
您可以选择在sqlite数据库中启用外键。它与…和不相似?我必须将“ent\U id”列放在第二列中?好的,回调函数参数Schema::table中缺少Blueprint::class,该参数会发生绝对变化…无。。。这很奇怪,因为如果我删除用于添加外部约束的ligne,我运行迁移,并在phpMyAdmin中手动添加外键,那么这样做没有问题。。。
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->string('title');
$table->string('slug')->unique();
$table->string('image')->default('default.png');
$table->string('zip');
$table->text('body');
$table->integer('view_count')->default(0);
$table->boolean('status')->default(false);
$table->boolean('is_approved')->default(false);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();