PHP PDO - Inserting into table with foreign keys - php

I want to insert into a table called Festivals that has two foreign keys (venueID & ticketID).
The SQL:
$addFestivalSql = "INSERT INTO festivals
VALUES(DEFAULT, :festivalName, :festivalDescription,
:festivalImage, :festivalLineup,
:musicTypeId, DEFAULT, DEFAULT)";
The error:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
The Festivals Table:
The Festivals Table
I've created the two foreign key constraints with venueID and ticketID relating to the two primary keys.
Why do you think I'm getting this error?
Thanks.

Related

How to bypass/disable Foreign Key Constraints on Laravel Queries

Laminas\Db\Adapter\Exception\InvalidQueryException
Statement could not be executed (23000 - 1452 - Cannot add or update a child row: a foreign key constraint fails (lms.student_course_logs, CONSTRAINT student_course_logs_lecture_id_foreign FOREIGN KEY (lecture_id) REFERENCES courses (id) ON DELETE CASCADE))
http://localhost/public/student/course/loglecture
I'm having this foreign key constraints in my query. How do I solve this one? Would it be possible to bypass foreign key contraints?
Here's my error log: https://flareapp.io/share/omwaaZR7#F51
Thank you.

Laravel SQLSTATE[23000]: Integrity constraint violation: 1452

I am facing an integrity constraints issue when adding a new row to a table has relation with another table (children and visits tables) every child has many visits.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (vaccine.visits, CONSTRAINT visits_ch_id_foreign FOREIGN KEY (ch_id) REFERENCES childern (id)) (SQL: insert into visits (ch_id, ce_id, emp_id, next_visit, notes, updated_at, created_at) values (202151-15, 1, 2, 2021-05-21, this is a test, 2021-05-17 07:30:48, 2021-05-17 07:30:48))
I am understanding the error message that should the value in the foreign key (ch_id) in the visits table should be found in the primary key (id) in the children key.
The problem is the value already exists in the primary table but I am getting the same error
Here is my code and screenshot for the database
$visit=new Visit();
$visit->ch_id=$child_id;
$visit->ce_id=1;
$visit->emp_id=2;
$visit->next_visit=$request->input('next_visit_date');
$visit->notes=$request->input('visit_notes');
$result=$visit->save();
I am testing on value 202151-15 and it already exists on the children table as following:

Seeding Laravel many to many table with data

I have 3 tables; orders, products and order_product. I am trying to seed the order_product pivot table using the Seeder class.
This is what I have in my code;
$productArray = \Illuminate\Support\Facades\DB::table('products')->pluck('id')->toArray();
factory(App\Order::class, 60)->create()->each(function ($order) use ($productArray) {
$randomPickings = mt_rand(1, 4);
$order->products()->sync(array_rand($productArray, $randomPickings));
});
I want attach product ids from the $productArray so an order can have, say, 1,2,3,4 or 5 products attached after seeding. Currently, I get this error when I run the seeder.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(`foo`.`order_product`, CONSTRAINT
`order_product_product_id_foreign` FOREIGN KEY (`product_id`)
REFERENCES `products` (`id`) ON DELETE CASCADE) (SQL: insert into
`order_product` (`order_id`, `product_id`) values (2, 0))
There's no product with id 0. I'm guessing it's picking the indexes instead of the values. How do I get the values selected/picked instead? Thanks.

Mysql foreign key

so i am trying again and again to make a column as a foreign key but it gives me Constraint name error? what Constraint name should i put? i am using PHPmyadmin SQL , I tried giving it names such as trackid and so on but still i am getting an error
ALTER TABLE `ss_ordered_carts`
ADD CONSTRAINT `track_id` FOREIGN KEY (`track_id`)
REFERENCES `ProjectDatabase`.`ss_orders` (`track_id`)
ON DELETE RESTRICT ON UPDATE CASCADE;
MySQL said: Documentation
1452 - Cannot add or update a child row: a foreign key constraint fails (projectdatabase.
'#sql-1414_6c7'>, CONSTRAINT track_id FOREIGN KEY (track_id) REF
ERENCES ss_orders (track_id) ON UPDATE CASCADE)
If you have data in child table for the referred column field, to add foreign key on that field,
first you have to disable foreign key checking.
And then alter to add foreign key.
And lastly reset the constraint checking.
Step 1: Disable the foreign key checks.
set foreign_key_checks=0;
Step 2: Now add the foreign key constraint.
ALTER TABLE `ss_ordered_carts`
ADD CONSTRAINT `track_id` FOREIGN KEY (`track_id`)
REFERENCES `ProjectDatabase`.`ss_orders` (`track_id`)
ON DELETE RESTRICT ON UPDATE CASCADE;
Step 3: Now reset the foreign key checks.
set foreign_key_checks=1;
It is possible that you have table ss_ordered_carts filled with wrong data, that cannot be related. Check it using this query -
SELECT * FROM ss_ordered_carts t1
LEFT JOIN ss_orders t2
ON t1.track_id = t2.track_id
WHERE
t2.track_id IS NULL;
Then modify table's data, and after, create foreign key.
Try this.
mysql> SET foreign_key_checks = 0;
mysql> ALTER TABLE ss_ordered_carts
ADD CONSTRAINT track_id
FOREIGN KEY(track_id)
REFERENCES ss_orders(track_id)
mysql> SET foreign_key_checks = 1;
May be, you are getting the specified error as there exists data in the tables.
This may also occur if child table contain some data with the foreign key that that are not in parent table. In this case try,
Delete from child where child_id NOT IN (select parent_id from parent where parent.id = child.id)

error while inserting in codeigniter

I am getting this error while inserting in my table:
A Database Error Occurred
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`giftme`.`giftme_shop`, CONSTRAINT `shop_ibfk_2` FOREIGN KEY (`sh_cid`) REFERENCES `giftme_shop_category` (`sc_cid`) ON DELETE CASCADE)
INSERT INTO `giftme_shop` (`sh_uid`, `sh_shop_name`, `sh_cid`) VALUES ('79', 'Naveen Reddys shop', 1)
Filename: /var/www/apps/helpers/sessions_helper.php
Line Number: 55
Any solution please?
there is foreign key reference
parent : `giftme_shop_category`.`sc_cid`
child : `giftme_shop`.sh_cid
there is no value of sc_cid=1 in parent table
Remove Foreign Key constraint
ALTER TABLE giftme_shop DROP FOREIGN KEY shop_ibfk_2;
read here
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol

Categories