Home > Blockchain >  Error executing migration with foreign key string
Error executing migration with foreign key string

Time:11-05

I have these two migrations

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

When I run I get the following error View image

CodePudding user response:

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

The easiest way is to chain unique function.

    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();
    });
  • Related