Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX][php]: issue when running php artisan migrate --seed #617

Open
cyrilmano opened this issue Feb 26, 2025 · 4 comments
Open

[FIX][php]: issue when running php artisan migrate --seed #617

cyrilmano opened this issue Feb 26, 2025 · 4 comments
Assignees

Comments

@cyrilmano
Copy link

cyrilmano commented Feb 26, 2025

Description:

This issue happened when running the php artisan migrate --seed
The CategoriesTableSeeder tried to truncate the categories table. check screenshot of image below.

Image
Image

Image

Expected Behavior:

The truncating during seeding after the migration, failed due to foreign key constraints on category_product.category_id_foreign

Below the error message;

Database\Seeders\CategoriesTableSeeder .................................................................... RUNNING  

   Illuminate\Database\QueryException 

  SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (`hng_boilerplate`.`category_product`, CONSTRAINT `category_product_category_id_foreign`) (Connection: mysql, SQL: truncate table `categories`)

  at vendor\laravel\framework\src\Illuminate\Database\Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

  1   vendor\laravel\framework\src\Illuminate\Database\Connection.php:587
    828▕
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }
    828▕
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    828▕
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    828▕
  ➜ 829▕             throw new QueryException(
    828▕
    828▕
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

### Changes to be Made

  1. Disable foreign key checks before truncating
  2. Use Delete Instead of Truncate
  3. Then after which enable foreign key checks after truncating
@cyrilmano
Copy link
Author

Expected Behavior: The truncating failed due to foreign key constraints on category_product.category_id_foreign

@cyrilmano cyrilmano changed the title issue when running php artisan migrate --seed FIX: issue when running php artisan migrate --seed Feb 26, 2025
@cyrilmano cyrilmano changed the title FIX: issue when running php artisan migrate --seed [FIX][php]: issue when running php artisan migrate --seed Feb 26, 2025
@cyrilmano
Copy link
Author

Issue created by: [email protected]
Slack Display Name: ThankGod Cyril Uche on LinkedIn

@omotomiwa26
Copy link
Contributor

Use This Issue Template

@cyrilmano
Copy link
Author

Use This Issue Template

Thank you for the feedback. And I have made used the template as a guide to make adjustment to the description, expected behaviour, and also use the <> code formatting for the error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants