samedi 23 décembre 2017

Laravel adding foreign key and get Cannot add or update a child row error

for this migration file as payments table i want to add other column with setting foreign key in other migration file, after run command in terminal i get this error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`laravel`.`#sql-1f1_42b`, CONSTRAINT `payments_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DE
  LETE CASCADE) (SQL: alter table `payments` add constraint `payments_product_id_foreign` foreign key (`product_id`) references `
  products` (`id`) on delete cascade)

my payments migration file:

public function up()
{
    Schema::create('payments', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('resnumber');
        $table->string('price');
        $table->boolean('payment')->default(false);
        $table->timestamps();
    });
}

and i want to add product_id column with this file:

class SetProductIdForeignToPayment extends Migration
{
    public function up()
    {
        Schema::table('payments', function (Blueprint $table) {
            $table->integer('product_id')->unsigned()->index()->default(0);
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        });

    }
    public function down()
    {

    }
}

how can i resolve this problem?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2C5LJwG
via IFTTT

Aucun commentaire:

Enregistrer un commentaire