【问题标题】:Error executing migration with foreign key string使用外键字符串执行迁移时出错
【发布时间】:2021-11-03 20:05:41
【问题描述】:

我有这两个迁移

tb_store

        Schema::create('tb_store', function (Blueprint $table) {
            $table->integer('cnpj')->unsigned();
            $table->string('email', 255);
            $table->string('password', 255);
            $table->string('corporateName', 255);
            $table->primary(['cnpj', 'email']);
            $table->timestamps();
            $table->softDeletes();
        });

tb_store_address

        Schema::create('tb_store_address', function (Blueprint $table) {
            $table->string('email', 255);
            $table->foreign('email')->references('email')->on('tb_store')->cascadeOnDelete();
            $table->integer('cnpj')->unsigned();
            $table->foreign('cnpj')->references('cnpj')->on('tb_store')->cascadeOnDelete();
            $table->primary(['cnpj', 'email']);
            $table->string('address', 255);
            $table->integer('number')->unsigned();
            $table->integer('phone')->unsigned();
            $table->integer('postalCode')->unsigned();
            $table->string('neighborhood', 255);
            $table->string('complement', 255)->nullable();
            $table->integer('idCity')->unsigned();
            $table->foreign('idCity')->references('idCity')->on('tb_city')->cascadeOnDelete();
        });

运行时出现以下错误

【问题讨论】:

  • 该错误清楚地表明缺少索引。因此,您必须为提到的列设置索引。
  • @B001 我该怎么做?

标签: php laravel eloquent migration


【解决方案1】:

https://laravel.com/docs/8.x/migrations#creating-indexes

最简单的方法是链接唯一函数。

    Schema::create('tb_store', function (Blueprint $table) {
        $table->integer('cnpj')->unsigned()->unique();
        $table->string('email', 255)->unique();
        $table->string('password', 255);
        $table->string('corporateName', 255);
        $table->primary(['cnpj', 'email']);
        $table->timestamps();
        $table->softDeletes();
    });

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 2016-05-08
    • 2015-03-31
    • 2014-11-03
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多