SQLSTATE[23000]: Integrity constraint violation: 1048 laravel

随声附和 提交于 2021-01-29 10:42:20

问题


I'm getting the following MYSQL error while trying to attach products to an order:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'order_id' cannot be null (SQL: insert into order_product (amount, history_price, order_id, product_id) values (1, 5.35, , ))

I've already searched for a solution, however all solutions I managed to find seemed to talk about not using default laravel id's as primary keys, while I am using default ID's.

Here are the three migrations:

Schema::create('orders', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('status_id')->unsigned();
        $table->boolean('visible')->default(true);
        $table->string('postal_code');
        //$table->string('country');
        $table->string('municipality');
        $table->string('street');
        $table->string('house_number');

        $table->timestamps();
    });

    Schema::table('orders', function($table) {
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

        $table->foreign('status_id')
            ->references('id')
            ->on('statuses')
            ->onDelete('cascade');
    });

Schema::create('products', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('name', 100);
        $table->float('price');
        $table->integer('stock');
        $table->string('short_description', 240);
        $table->string('long_description', 1000);
        $table->boolean('visible')->default(true);
        $table->string('image_url',240);
        $table->timestamps();
    });

Schema::create('order_product', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->integer('order_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->float('history_price');
        $table->integer('amount');
        $table->timestamps();
    });

    Schema::table('order_product', function($table) {
        $table->foreign('order_id')
            ->references('id')
            ->on('orders')
            ->onDelete('cascade');

        $table->foreign('product_id')
            ->references('id')
            ->on('products')
            ->onDelete('cascade');

        $table->primary(array('order_id', 'product_id'));
    });

This is the code where the error comes from:

foreach($user->products()->get(['price','amount']) as $product){
        $order->products()->attach($product, array('history_price'=> $product->price, 'amount'=>$product->amount,'order_id'=>$order->id, 'product_id'=>$product->id));
}
$order->save();

If needed I could supply the model methods which define the relations between the two, however as these tables are following standard laravel conventions I don't think it's necessary.

来源:https://stackoverflow.com/questions/50782103/sqlstate23000-integrity-constraint-violation-1048-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!