就像其他人回答的那样,当您删除一行时,实际上没有必要将计数器往回移动.不过,您可以truncate
张桌子,其中will delete all the table rows张并重置计数器.
对于应用了Foreign Key Constraints
的表,不能使用truncate
(truncate
与delete
不同,delete
只是删除所有行,而keeping是自动递增计数器).
Hence, while using foreign key constrains
, MySQL might stop you from truncating a table which has foreign key constraints
applied to it.
您可以执行以下步骤来实现所需,但请注意,数据完整性可能存在风险.我只在testing purposes岁时用它.
编辑DatabaseSeeder
类(app/database/seeds/DatabaseSeeder.php
提供)如下:
<?php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
// Disable Foreign key check for this connection before running seeders
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
$this->call('UserTableSeeder');
// ...
// FOREIGN_KEY_CHECKS is supposed to only apply to a single
// connection and reset itself but I like to explicitly
// undo what I've done for clarity
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}
}
现在,Table Seeder类(本例中为UserTableSeeder
,应在app/database/seeds/UserTableSeeder.php
处创建)可以按如下方式调用截断表:
<?php
class UserTableSeeder extends Seeder {
public function run()
{
// Truncate the table.
DB::table('users')->truncate();
// The auto-increment has been reset.
// Now we can start adding users.
User::create(
array(
'email' => 'example@domain.com',
'password' => Hash::make('test')
)
);
}
}