I used this in my update function:
$qty = $request->input('checked_out_qty');
DB::table('consumables')->decrement('remaining_qty',$qty );
i keep getting this error:
"SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (fixed.consumables_histories, CONSTRAINT 251075_5c35eb169cb40 FOREIGN KEY (checked_out_qty_id) REFERENCES consumables (id) ON DELETE CASCADE) (SQL: insert into consumables_histories (consumables_id, checked_out_qty_id, checked_out_by_id, status_id, location_id, assigned_to_id, updated_at, created_at) values (1368, 6, 1, 1, 1, , 2019-02-03 08:08:34, 2019-02-03 08:08:34))
but when u refresh the second time, the decrement still happens. what could i be doing wrong?
Related
I tried this
CreatedPlan::where('meal_type_id',config('const.PLAN.__MORNING_MEAL'))->whereYear('calendar_date', $targetYear)->whereMonth('calendar_date',$targetMonth)->get();
However following error happened.
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (plan.created_plans_foods, CONSTRAINT created_plans_foods_created_plan_id_foreign FOREIGN KEY (created_plan_id) REFERENCES created_plans (id)) (SQL: delete from created_plans where __id = 1 and year(calendar_date) = 2022 and month(calendar_date) = 04) in file /Users/Developments/plan/bb/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 669
I tried to delete the records of the related table.
The problem is that you are deleting the record of the parent table first and then the child table. This is why laravel gives you the error.
In order to delete this type of case first delete the child table data and then delete the parent table data.
Suppose you have table name users and the other is users_info. The user id is the parent key in the users_info table. so do the following :
First delete from user_info
UserInfo::where('user_id',$userId)->delete();
Then delete it from the users. Hope this will solve your problem .
User::where('id',$userId)->delete();
I am using laravel
My Error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`bd_name`.`inpatients`, CONSTRAINT `inpatients_ward_id_foreign` FOREIGN KEY (`ward_id`) REFERENCES `wards` (`id`)) (SQL: insert into `inpatients` (`patient_id`, `ward_id`, `updated_at`, `created_at`) values (2010101, 06, 2020-10-14 06:44:12, 2020-10-14 06:44:12))
My inpatient table is here:
$table->bigIncrements('id');
$table->timestamps();
$table->unsignedBigInteger('patient_id');
$table->foreign('patient_id')->references('id')->on('patients');
$table->char('discharged',4)->default('NO'); // YES | NO
$table->unsignedBigInteger('ward_id');
$table->foreign('ward_id')->references('id')->on('wards');
inpatientController
$INPtable = new inpatient;
$INPtable->patient_id = $request->reg_pid;
$INPtable->ward_id = $request->reg_ipwardno;
Inpatient Model
protected $fillable = [
'ward_id','patient_id',
];
public function ward() {
return $this->belongsTo('App\Ward');
}
public function patient() {
return $this->belongsTo('App\Patients');
}
I don't understand what's going wrong here. Please help me.
The error will be mostly because of the table which you referred as foreign key in inpatient table i.e ward table. You first have to enter data in the ward table, and that data you can refer in the child table. If key 6, is not present in the ward table, insert it first into the table, then do the insertion in the inpatient table.
Hope you got it. Thank You :)
My ward table has ward_no also. I have passed ward_no instead of ward id. That's why the error.
I create a table- GAllERY_ID on POST. That is a foreign key nullable, and null as default.
When I create a POST, if I don't set the GALLERY_ID, it will be null.
But if I set the GALLERY_ID and reference on a GAllery, after if I try to make this NULL again (ON UPDATE ), I can´t.
I did like this the migration -
UP –
$table->integer(‘gallery_id’)->unsigned()->nullable()->default(null);
$table->foreign(‘gallery_id’)->references(‘id’)->on(‘photo_gallery’);
DOWN –
$table->dropForeign(‘posts_gallery_id_foreign’);
Have a look on the image of my DB on PHP MY ADMIN
If I go on PHP my admin I can update this value, just checking NULL on Gallery ID, BUT in my code on UPDATE, if I set like this -
$post->gallery_id = Null;
It will give this error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ...
Because I can't set null a foreign key to Update, just on a created method, if I don't set, it will be null, but if I set a value and after I try to change to NULL, I can't.
Some one can help me?
I have one simple table:
-- Create table
create table FAVOURITE_RULES
( rule_id NUMBER(9) not null,
user_id NUMBER(9) not null);
-- Create/Recreate primary, unique and foreign key constraints
alter table FAVOURITE_RULES
add constraint FAV_RULES_PK primary key (RULE_ID, USER_ID)
alter table FAVOURITE_RULES
add constraint FAV_RULES_RULE_ID_FK foreign key (RULE_ID)
references RULES (RULE_ID) on delete cascade;
alter table FAVOURITE_RULES
add constraint FAV_RULES_USER_ID foreign key (USER_ID)
references USER_AUTHENTICATION (USER_ID) on delete cascade;
I have a rule (from .Net code) that doesn't exist in the original table RULES. It has the Id=-999.
When I try to insert into the FAVOURITE_RULES I get an error about integrity constraint violation (as expected) (FAV_RULES_RULE_ID_FK) violated - parent key not found.
Can I keep the foreign key (FAV_RULES_RULE_ID_FK ) and allow extra only this value (-999) to be inserted?
May be this can help.
Step 1: drop fk constraint
Step 2: insert your violating row
Step 3: again create fk constraint with ENABLE NOVALIDATE
$job->created_by = $input['created_by'];
I want to pass user id in this array which is in other table what should I do?
The field is a foreign key.
When I run this it throws an exception
SQLSTATE[23000]: Integrity constraint violation: 1452
Cannot add or update a child row: a foreign key constraint fails
(`freight`.`jobs`, CONSTRAINT `approved_by` FOREIGN KEY (`created_by`)
REFERENCES `users` (`id`))
(SQL: insert into `jobs`
(`company_id`, `origin`, `commodity`, `destination`,
`created_by`, `approved_by`, `date`, `carrier`, `consolidator`,
`overseas_agt`, `prepaid_fob`, `free_time`, `wt_pcs`,
`updated_at`, `created_at`)
values (2, , , , asdsadasdsad, asdsadasdsad, , , , , , , ,
2015-11-26 07:32:02, 2015-11-26 07:32:02)
)
You have initialize the field as unsigned in the DB Migration.
as:
$table->integer('user_id')->unsigned();
Here user_id is a primary key of another table.
Alter the table and run migration again.
It will work definitely.
I found what I did wrong I have to give the id of the user that is currently active the correct way is as follow
$job->created_by = \Auth::user()->id;