【问题标题】:(Resolved) Laravel 6.2 : adding foreign key constraint failed during migration(已解决)Laravel 6.2:迁移期间添加外键约束失败
【发布时间】:2020-05-20 21:58:42
【问题描述】:

我正在尝试在 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`))

我的“企业”迁移代码

 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
    });

我更改了迁移的顺序,并且在“用户”表之前创建了“企业”表。

我尝试了herehere 的所有解决方案,它们没有什么可改变的。

有什么建议吗?

编辑解决方案

真丢脸……当我引用我想引用的表格时,我犯了一个愚蠢的错误……现在一切都很好……对不起

【问题讨论】:

标签: mysql laravel eloquent migration laravel-6.2


【解决方案1】:

您使用的是 sqlite 数据库吗?如果是这样,sqlite 数据库默认禁用外键功能。

SQLite 默认禁用外键约束。使用 SQLite 时,请确保在您的数据库配置中启用外键支持,然后再尝试在迁移中创建它们。

你可以勾选here在sqlite数据库中启用外键。

【讨论】:

  • 好的,你在回调函数参数 Schema::table 中缺少 Blueprint::class
  • 那改变绝对......没什么......这很奇怪,因为如果我删除 ligne 以添加外部约束,我运行迁移并在我的 phpMyAdmin 中手动添加外键,我没有问题去做吧……
【解决方案2】:
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();

【讨论】:

  • 然后?我必须将“ent_id”列放在第二列?
猜你喜欢
  • 2016-01-20
  • 1970-01-01
  • 2019-03-24
  • 2017-03-27
  • 2018-04-01
  • 1970-01-01
  • 2021-08-30
  • 2020-09-21
  • 2020-03-12
相关资源
最近更新 更多