I have an integrity constraint violation when I run the php artisan db:seed command in Laravel 9. Error PDOException::(“SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (hunger-db.restaurants, CONSTRAINT restaurants_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE)”) setup_users_table setum_restaurants_table User Model Restaurant Model Answer I see some things to change
Tag: foreign-keys
Determine name of table referenced by a foreign key from another table using PHP?
Just learning mysql and php and having to create a test project for my exam. How do I determine the name of a table (target) a foreign key from another table (source) references, and how would I go about that with PHP? I have been googling this as well as browsing SO for it for hours, but have not found
How to add constraints to existing keys using laravel migration
I currently have a database in my project which has already been put in production. But I didn’t use constraints in the previous migrations. Now I have tables products, shops and an intermediate table product_shop. The problem is that if I delete any product, that has been put on some shop, the pivot is still kept in the intermediate table.
Foreign key codes are not working in migrations of Laravel 8
I tried to make a foreign key, however, it is not working. Posts Migration Users Migration I am trying to make a “user_id” column for posts that should be connected with an id of a user via a foreign key. But, when I migrate I am getting errors like the following IlluminateDatabaseQueryException SQLSTATE[HY000]: General error: 1005 Can’t create table mytest_db.posts
Inserting data into a table using foreign keys in MySQL
I am trying to insert data into a contacts table using foreign keys in mysql, the structure of tables are as follows CONTACTS TABLE id contactname phone_number fk_id USERS TABLE pers_id …
Inserting to multiple mysql table based on an primary key from first table
I am trying to write to two mysql tables. Table 1: vehicles The field vehicle_id is auto incremented. I need to use this field in the next table speed_log. This is the other table that. Table 2: speed_log As above, the id is auto incremented but I need to pick the vehicle_id from the first table when the script runs.
Laravel 7 migration error. Cannot add foreign key constraint
Try to create foreign key in Laravel 7 but when I migrate tables using artisan it gives error SQLSTATE[HY000]: General error: 3780 Referencing column ‘remote_id’ and referenced column ‘parent_id’ in …
left join query of mysql php
I’m working on a project where i have two tables 1 is the Users table and another one in the Department table. here i have given foreign key connection to department table to the users table and …
Add foreign key to a migration (Laravel)
I´ve spent the whole day trying to figure out what is happening with the following migration. This the migration of the table. My idea is to use ‘id_stays_abroad’ and ‘user_id’ as foreign keys. …