0

I am working with an e-commerce db. My task is to make CRUD ops for products and product variants. Here'sthe db I'm working with:

enter image description here

When updating product_option_values everything works properly. But when updating product_options which results in updating the previously mentioned tables causes an Integrity constraint violation: 1452.

enter image description here

I am using filament package for laravel which handles creating and updating records.

Any help would be appreciated. Thanks in advance.

  • Please [edit] and post your code instead of Screenshot – STA Oct 21 '22 at 05:51
  • 2
    This is usually caused by there not being a record in the parent table with the corresponding ID. A parent record must be present before the child record. Can you provide any code that is relevant to how records are added or updated? – Peppermintology Oct 21 '22 at 06:13
  • I included 0 codes in my code. You mean db tables migrations? – Bashar El-Mouhammad Oct 21 '22 at 06:13
  • Peppermintology, filament takes the wheel for updating and creating records. I am trying to solve by manipulating the data sent to the db. – Bashar El-Mouhammad Oct 21 '22 at 06:16
  • Do you get this error when trying to create, or update records? You need to ensure Filament is creating a record in the product_options table before it creates a record in the product_options_values table. I don’t personally use Filament so can’t provide an example for how to do that. – Peppermintology Oct 21 '22 at 06:22
  • @BasharEl-Mouhammad Try running these 2 queries in [Laravel tinker](https://laravel.com/docs/9.x/artisan#tinker). If any of these queries return an empty resultset, that will confirm that you have orphan records in your database. 1. `\Illuminate\Support\Facades\DB::select('select * from variants where id = 21');` 2. `\Illuminate\Support\Facades\DB::select('select * from product_option_values where id = 5');` – steven7mwesigwa Oct 21 '22 at 06:26
  • To view all records in the `variant_product_option_value` table that *luck* a referenced record in the respective *parent tables*, run these two queries. 1. `\Illuminate\Support\Facades\DB::select('select * from variant_product_option_value where variant_id not in (select id from variants)');` 2. `\Illuminate\Support\Facades\DB::select('select * from variant_product_option_value where product_option_value_id not in (select id from product_option_values)');` – steven7mwesigwa Oct 21 '22 at 06:43
  • I get this error when updating records usually – Bashar El-Mouhammad Oct 21 '22 at 06:47
  • If you don't add the code, we will not be able (or willing) to answer. – Gert B. Oct 21 '22 at 06:49
  • @GertB. The OP is most likely using the [filament](https://filamentphp.com/docs/2.x/admin/installation) package. Unfortunately, I've never used it before. He probably should find out why the third-party package is submitting *"orphan records"* in the database to begin with. – steven7mwesigwa Oct 21 '22 at 06:55
  • I appreciate y'all. I am testing the application on multiple steps to see what data gets added to db. So if I ever needed a help ill get back to here. – Bashar El-Mouhammad Oct 21 '22 at 07:21

1 Answers1

0

The fix was a DB update. So basically I was creating/adding to the product_variant_option_value while product_option_value weren't added already. So I updated the DB. Now from the same form the user can add options and values then ill make the combination of option values for each variant then the relationship will be saved. For those who don't use filament. Filament is a powerful tool to create dashboards.

enter image description here

  • 1
    Creating the `product_options` before the `product_option_values` was literally the first comment I made on your question. I don't think a database change is/was required, simply changing the order in which the records are created would have solved it. At least you've found a resolution though. – Peppermintology Oct 21 '22 at 09:17
  • @Peppermintology Appreciate your time. Thanks again. – Bashar El-Mouhammad Oct 21 '22 at 10:26