Laravel Cannot add or update a child row: a foreign key constraint fails
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Before you start giving vote-down or mark as duplicated please read:
I have read Cannot add or update a child row a foreign key constraint fails and it is not my case,
- I have existing record
- my table supports cascade on delete.
- I also have my unsigned columns nullable
-- codes provided below
Issue
I am trying save related data in 3rd table named product_attributes
with sync
method, which gets product_id
and attribute_id
Code
schema
public function up()
{
Schema::create('product_attributes', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->nullable()->unsigned();
$table->integer('attribute_id')->nullable()->unsigned();
});
Schema::table('product_attributes', function (Blueprint $table) {
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
});
}
product model
public function attributes(){
return $this->belongsToMany(Attribute::class, 'product_attributes', 'product_id', 'attribute_id');
}
attribute model
public function products(){
return $this->belongsToMany(Product::class);
}
product controller
$product->save()
$product->attributes()->sync($request->attributes, false);
blade
<select class="form-control" name="attributes" id="attributes" multiple="multiple">
@foreach($attributes as $attribute)
<option value="{{ $attribute->id }}">{{ $attribute->title }}</option>
@endforeach
</select>
Error
this is what I'm getting:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shopping`.`product_attributes`, CONSTRAINT `product_attributes_attribute_id_foreign` FOREIGN KEY (`attribute_id`) REFERENCES `attributes` (`id`) ON DELETE CASCADE) (SQL: insert into `product_attributes` (`attribute_id`, `product_id`) values (*parameters, 3))
Anyone can help with that?
Update
Here is what I get when I send my data to controller
php laravel synchronization
add a comment |
Before you start giving vote-down or mark as duplicated please read:
I have read Cannot add or update a child row a foreign key constraint fails and it is not my case,
- I have existing record
- my table supports cascade on delete.
- I also have my unsigned columns nullable
-- codes provided below
Issue
I am trying save related data in 3rd table named product_attributes
with sync
method, which gets product_id
and attribute_id
Code
schema
public function up()
{
Schema::create('product_attributes', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->nullable()->unsigned();
$table->integer('attribute_id')->nullable()->unsigned();
});
Schema::table('product_attributes', function (Blueprint $table) {
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
});
}
product model
public function attributes(){
return $this->belongsToMany(Attribute::class, 'product_attributes', 'product_id', 'attribute_id');
}
attribute model
public function products(){
return $this->belongsToMany(Product::class);
}
product controller
$product->save()
$product->attributes()->sync($request->attributes, false);
blade
<select class="form-control" name="attributes" id="attributes" multiple="multiple">
@foreach($attributes as $attribute)
<option value="{{ $attribute->id }}">{{ $attribute->title }}</option>
@endforeach
</select>
Error
this is what I'm getting:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shopping`.`product_attributes`, CONSTRAINT `product_attributes_attribute_id_foreign` FOREIGN KEY (`attribute_id`) REFERENCES `attributes` (`id`) ON DELETE CASCADE) (SQL: insert into `product_attributes` (`attribute_id`, `product_id`) values (*parameters, 3))
Anyone can help with that?
Update
Here is what I get when I send my data to controller
php laravel synchronization
Seems about right. Except for your->products()
relationship, but that shouldn't matter in the code you posted. I'd double check the value of$request->attributes
just before the sync:dd($request->attributes)
Seems like it's "*parameters", which just seems weird
– devk
Jan 3 at 6:18
@devk exactly, when i dd request->attributes it gives me empty parameters but you see in my dd request->all() i have those values.
– mafortis
Jan 3 at 6:38
1
@mafortis try with$request->get('attributes')
– Jinal Somaiya
Jan 3 at 6:40
@JinalSomaiya it's working now, thank you. But why isn't working in default code? the code i shared in my question I used in 4 applications before this is the first time i have to use it with get('')?!
– mafortis
Jan 3 at 6:48
@mafortis read this for more info. laraveldaily.com/…
– Jinal Somaiya
Jan 3 at 6:57
add a comment |
Before you start giving vote-down or mark as duplicated please read:
I have read Cannot add or update a child row a foreign key constraint fails and it is not my case,
- I have existing record
- my table supports cascade on delete.
- I also have my unsigned columns nullable
-- codes provided below
Issue
I am trying save related data in 3rd table named product_attributes
with sync
method, which gets product_id
and attribute_id
Code
schema
public function up()
{
Schema::create('product_attributes', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->nullable()->unsigned();
$table->integer('attribute_id')->nullable()->unsigned();
});
Schema::table('product_attributes', function (Blueprint $table) {
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
});
}
product model
public function attributes(){
return $this->belongsToMany(Attribute::class, 'product_attributes', 'product_id', 'attribute_id');
}
attribute model
public function products(){
return $this->belongsToMany(Product::class);
}
product controller
$product->save()
$product->attributes()->sync($request->attributes, false);
blade
<select class="form-control" name="attributes" id="attributes" multiple="multiple">
@foreach($attributes as $attribute)
<option value="{{ $attribute->id }}">{{ $attribute->title }}</option>
@endforeach
</select>
Error
this is what I'm getting:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shopping`.`product_attributes`, CONSTRAINT `product_attributes_attribute_id_foreign` FOREIGN KEY (`attribute_id`) REFERENCES `attributes` (`id`) ON DELETE CASCADE) (SQL: insert into `product_attributes` (`attribute_id`, `product_id`) values (*parameters, 3))
Anyone can help with that?
Update
Here is what I get when I send my data to controller
php laravel synchronization
Before you start giving vote-down or mark as duplicated please read:
I have read Cannot add or update a child row a foreign key constraint fails and it is not my case,
- I have existing record
- my table supports cascade on delete.
- I also have my unsigned columns nullable
-- codes provided below
Issue
I am trying save related data in 3rd table named product_attributes
with sync
method, which gets product_id
and attribute_id
Code
schema
public function up()
{
Schema::create('product_attributes', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->nullable()->unsigned();
$table->integer('attribute_id')->nullable()->unsigned();
});
Schema::table('product_attributes', function (Blueprint $table) {
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
});
}
product model
public function attributes(){
return $this->belongsToMany(Attribute::class, 'product_attributes', 'product_id', 'attribute_id');
}
attribute model
public function products(){
return $this->belongsToMany(Product::class);
}
product controller
$product->save()
$product->attributes()->sync($request->attributes, false);
blade
<select class="form-control" name="attributes" id="attributes" multiple="multiple">
@foreach($attributes as $attribute)
<option value="{{ $attribute->id }}">{{ $attribute->title }}</option>
@endforeach
</select>
Error
this is what I'm getting:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shopping`.`product_attributes`, CONSTRAINT `product_attributes_attribute_id_foreign` FOREIGN KEY (`attribute_id`) REFERENCES `attributes` (`id`) ON DELETE CASCADE) (SQL: insert into `product_attributes` (`attribute_id`, `product_id`) values (*parameters, 3))
Anyone can help with that?
Update
Here is what I get when I send my data to controller
php laravel synchronization
php laravel synchronization
edited Jan 3 at 5:11
mafortis
asked Jan 3 at 5:02


mafortismafortis
1,232945
1,232945
Seems about right. Except for your->products()
relationship, but that shouldn't matter in the code you posted. I'd double check the value of$request->attributes
just before the sync:dd($request->attributes)
Seems like it's "*parameters", which just seems weird
– devk
Jan 3 at 6:18
@devk exactly, when i dd request->attributes it gives me empty parameters but you see in my dd request->all() i have those values.
– mafortis
Jan 3 at 6:38
1
@mafortis try with$request->get('attributes')
– Jinal Somaiya
Jan 3 at 6:40
@JinalSomaiya it's working now, thank you. But why isn't working in default code? the code i shared in my question I used in 4 applications before this is the first time i have to use it with get('')?!
– mafortis
Jan 3 at 6:48
@mafortis read this for more info. laraveldaily.com/…
– Jinal Somaiya
Jan 3 at 6:57
add a comment |
Seems about right. Except for your->products()
relationship, but that shouldn't matter in the code you posted. I'd double check the value of$request->attributes
just before the sync:dd($request->attributes)
Seems like it's "*parameters", which just seems weird
– devk
Jan 3 at 6:18
@devk exactly, when i dd request->attributes it gives me empty parameters but you see in my dd request->all() i have those values.
– mafortis
Jan 3 at 6:38
1
@mafortis try with$request->get('attributes')
– Jinal Somaiya
Jan 3 at 6:40
@JinalSomaiya it's working now, thank you. But why isn't working in default code? the code i shared in my question I used in 4 applications before this is the first time i have to use it with get('')?!
– mafortis
Jan 3 at 6:48
@mafortis read this for more info. laraveldaily.com/…
– Jinal Somaiya
Jan 3 at 6:57
Seems about right. Except for your
->products()
relationship, but that shouldn't matter in the code you posted. I'd double check the value of $request->attributes
just before the sync: dd($request->attributes)
Seems like it's "*parameters", which just seems weird– devk
Jan 3 at 6:18
Seems about right. Except for your
->products()
relationship, but that shouldn't matter in the code you posted. I'd double check the value of $request->attributes
just before the sync: dd($request->attributes)
Seems like it's "*parameters", which just seems weird– devk
Jan 3 at 6:18
@devk exactly, when i dd request->attributes it gives me empty parameters but you see in my dd request->all() i have those values.
– mafortis
Jan 3 at 6:38
@devk exactly, when i dd request->attributes it gives me empty parameters but you see in my dd request->all() i have those values.
– mafortis
Jan 3 at 6:38
1
1
@mafortis try with
$request->get('attributes')
– Jinal Somaiya
Jan 3 at 6:40
@mafortis try with
$request->get('attributes')
– Jinal Somaiya
Jan 3 at 6:40
@JinalSomaiya it's working now, thank you. But why isn't working in default code? the code i shared in my question I used in 4 applications before this is the first time i have to use it with get('')?!
– mafortis
Jan 3 at 6:48
@JinalSomaiya it's working now, thank you. But why isn't working in default code? the code i shared in my question I used in 4 applications before this is the first time i have to use it with get('')?!
– mafortis
Jan 3 at 6:48
@mafortis read this for more info. laraveldaily.com/…
– Jinal Somaiya
Jan 3 at 6:57
@mafortis read this for more info. laraveldaily.com/…
– Jinal Somaiya
Jan 3 at 6:57
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016602%2flaravel-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016602%2flaravel-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Seems about right. Except for your
->products()
relationship, but that shouldn't matter in the code you posted. I'd double check the value of$request->attributes
just before the sync:dd($request->attributes)
Seems like it's "*parameters", which just seems weird– devk
Jan 3 at 6:18
@devk exactly, when i dd request->attributes it gives me empty parameters but you see in my dd request->all() i have those values.
– mafortis
Jan 3 at 6:38
1
@mafortis try with
$request->get('attributes')
– Jinal Somaiya
Jan 3 at 6:40
@JinalSomaiya it's working now, thank you. But why isn't working in default code? the code i shared in my question I used in 4 applications before this is the first time i have to use it with get('')?!
– mafortis
Jan 3 at 6:48
@mafortis read this for more info. laraveldaily.com/…
– Jinal Somaiya
Jan 3 at 6:57