Magento unable to add a product to a category - magento

Whenever I try to add a product to a category I get this error.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (db2018.catalog_category_product, CONSTRAINT FK_CAT_CTGR_PRD_PRD_ID_CAT_PRD_ENTT_ENTT_ID FOREIGN KEY (product_id) REFERENCES catalog_product_entity_oud (entity_id) ON DE), query was: INSERT INTO catalog_category_product (category_id,product_id,position) VALUES (?, ?, ?), (?, ?, ?)
Seems like there are no values being submitted?
Can anyone point me in the right direction to get this fixed?

Related

Many-to-many relationships in Laravel Model Factory

I am building a SaaS using Tenancy for Laravel and I am having issues creating model factories to seed the database. This is a 2-part question.
I have the following data structure for tenants:
User model
accounts(): $this->belongsToMany(Account::class);
Account model
users(): $this->belongsToMany(User::class);
venues(): $this->hasMany(Venue::class);
Venue model
account(): $this->belongsTo(Account::class); (using an account_id column on the venues table)
I also have an account_user pivot table to store the relationship between the User and Account models with account_id and user_id columns.
Part one of my question:
I have created factories for the models and I would now like to seed the database.
This is my code:
TenantSeeder.php
User::factory()->count(10)->hasAccounts(10)->create();
However when I run it, I get the following error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'account_id' cannot be null (SQL: insert into account_user (account_id, user_id) values (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2))
I tried to specify the tables at the belongsToMany Relationships of the User and Account model, however I still get this error.
I have also tried to add the relationship with the ->has() method of the Factory instead of the magic method, but I get the same results.
User::factory()->count(10)->has(Account::factory()->count(10))->create();
The second part of my question:
Is there a way to seed the Venues at the same time as the users and accounts?
Ie. something like this:
User::factory()
->count(10)
->has(
Account::factory()
->count(10)
->has(
Venue::factory()
->count(10)
)
)
->create();
In my VenueFactory class I am adding the account_id column's value like this:
return [
'account_id' => Account::factory(),
.....
]
This, however, throws the following error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'account_id' cannot be null (SQL: insert into `venues` (`account_id`, `updated_at`, `created_at`) values (?, 2023-02-14 12:35:40, 2023-02-14 12:35:40))
According to the documentation, however, this should work?
Any idea what could cause these two issues? Is it related to Tenancy?

How to delete related records on other table

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

Laravel observer Update Event in not Triggering

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?

Laravel foreign key nullable can not be null on UPDATE

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?

Passing an id to other controller

$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;

Resources