Skip to content

Commit 0a27365

Browse files
committed
feat: Add --skip-fk-checks option to disable foreign key checks
When seeding tables with foreign key constraints, use --skip-fk-checks to wrap delete/insert statements with SET FOREIGN_KEY_CHECKS=0/1. This prevents "foreign key constraint failed" errors. Based on PR #157 by @aomini, with improved option naming and implementation that doesn't require stub modifications.
1 parent ebbb5a3 commit 0a27365

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ Example:
205205
php artisan iseed users --noregister
206206
```
207207

208+
### skip-fk-checks
209+
By using --skip-fk-checks the generated seeder will include statements to disable foreign key checks before deleting/inserting and re-enable them afterwards. This is useful when seeding tables that have foreign key constraints, as it prevents "foreign key constraint failed" errors.
210+
211+
Example:
212+
```
213+
php artisan iseed users --skip-fk-checks
214+
```
215+
216+
**Note**: This option generates MySQL-specific `SET FOREIGN_KEY_CHECKS` statements.
217+
208218
### where
209219
Optional parameter which allows you to specify a SQL WHERE clause to filter the rows that will be included in the seed file. The WHERE clause should be provided as a string and will be applied directly to the SQL query.
210220

src/Orangehill/Iseed/Iseed.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ public function readStubFile($file)
6565
* @param string $prerunEvent
6666
* @param string $postrunEvent
6767
* @param bool $register
68+
* @param bool $skipFkChecks
6869
* @return bool
6970
* @throws Orangehill\Iseed\TableNotFoundException
7071
*/
71-
public function generateSeed($table, $prefix = null, $suffix = null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC', $whereClause = null, $register = true)
72+
public function generateSeed($table, $prefix = null, $suffix = null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC', $whereClause = null, $register = true, $skipFkChecks = false)
7273
{
7374
if (!$database) {
7475
$database = config('database.default');
@@ -109,7 +110,8 @@ public function generateSeed($table, $prefix = null, $suffix = null, $database =
109110
$prerunEvent,
110111
$postrunEvent,
111112
$indexed,
112-
$database
113+
$database,
114+
$skipFkChecks
113115
);
114116

115117
// Save a populated stub
@@ -243,9 +245,10 @@ public function getStubPath()
243245
* @param string $postrunEvent
244246
* @param bool $indexed
245247
* @param string $database
248+
* @param bool $skipFkChecks
246249
* @return string
247250
*/
248-
public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null, $indexed = true, $database = null)
251+
public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null, $indexed = true, $database = null, $skipFkChecks = false)
249252
{
250253
$chunkSize = $chunkSize ?: config('iseed::config.chunk_size');
251254

@@ -336,6 +339,22 @@ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $p
336339

337340
$stub = str_replace('{{insert_statements}}', $inserts, $stub);
338341

342+
// Add foreign key check disable/enable if requested
343+
if ($skipFkChecks) {
344+
// Find the delete statement and wrap everything with FK disable/enable
345+
$stub = preg_replace(
346+
'/(\\\\DB::.*->delete\(\);)/',
347+
"\\DB::statement('SET FOREIGN_KEY_CHECKS=0;');\n\n $1",
348+
$stub
349+
);
350+
// Add enable statement after the last insert
351+
$stub = preg_replace(
352+
'/(->insert\([^;]+\);)(?!.*->insert\()/',
353+
"$1\n\n \\DB::statement('SET FOREIGN_KEY_CHECKS=1;');",
354+
$stub
355+
);
356+
}
357+
339358
return $stub;
340359
}
341360

src/Orangehill/Iseed/IseedCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function fire()
7171
$dumpAuto = intval($this->option('dumpauto'));
7272
$indexed = !$this->option('noindex');
7373
$register = !$this->option('noregister');
74+
$skipFkChecks = $this->option('skip-fk-checks');
7475
$orderBy = $this->option('orderby');
7576
$direction = $this->option('direction');
7677
$prefix = $this->option('classnameprefix');
@@ -112,7 +113,8 @@ public function fire()
112113
$orderBy,
113114
$direction,
114115
$whereClause,
115-
$register
116+
$register,
117+
$skipFkChecks
116118
),
117119
$table
118120
);
@@ -137,7 +139,8 @@ public function fire()
137139
$orderBy,
138140
$direction,
139141
$whereClause,
140-
$register
142+
$register,
143+
$skipFkChecks
141144
),
142145
$table
143146
);
@@ -182,6 +185,7 @@ protected function getOptions()
182185
array('classnameprefix', null, InputOption::VALUE_OPTIONAL, 'prefix for class and file name', null),
183186
array('classnamesuffix', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', null),
184187
array('where', null, InputOption::VALUE_OPTIONAL, 'where clause to filter records', null),
188+
array('skip-fk-checks', null, InputOption::VALUE_NONE, 'disable foreign key checks during seeding', null),
185189
);
186190
}
187191

0 commit comments

Comments
 (0)