Skip to content

Seeding throws Exception on Schema with Foreign Key Constraints #243

Closed
@ghost

Description

If you attempt to use seeding on a table with foreign key constraints, an exception will be thrown as truncate operations are not permitted on tables with foreign keys.

That said, is truncate necessary as part of the seeding? If you are populating an empty database having run a migration, then there is no need to truncate as all of your tables are empty.

If you use php artisan migrate:refresh --seed then all your migrations will be rolled back and restarted - which again would mean that there is no need for a truncate operation since your tables are once again empty?

php artisan db:seed --env=local



  [Exception]                                                                  
  SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a t  
  able referenced in a foreign key constraint (`database`.`projects`, CON  
  STRAINT `projects_client_id_foreign` FOREIGN KEY (`client_id`) REFERENCES `  
  database`.`clients` (`id`)) (SQL: truncate `clients`) (Bindings: array   
  (                                                                            
  ))                                                                           



db:seed [--database[="..."]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions