Doctrine 2. orm:schema-tool:update.设置开始ID

min*_*ort 9 php fixtures doctrine-orm

当我使用./bin/doctrine orm:fixtures:load样本数据填充表时,首先迁移设置自动增量表ID,如1,2,3,4,5等...

在第二次orm:fixtures:load迁移命令后,它清除所有数据并设置ID,如5,6,7,8,9等等......

当我多次加载灯具时,如何将AI id计数器重置为1?

Tom*_*rei 20

$ app/console help doctrine:fixtures:load

默认情况下,Doctrine Data Fixtures使用DELETE语句从数据库中删除现有行.如果要使用TRUNCATE语句,则可以使用--purge-with-truncate标志:

./app/console doctrine:fixtures:load --purge-with-truncate
Run Code Online (Sandbox Code Playgroud)

截断将重置自动增量.

UPDATE

控制台命令适用于Symfony,但它应该只使用Doctrine相同:

./bin/doctrine orm:fixtures:load --purge-with-truncate
Run Code Online (Sandbox Code Playgroud)

有关抛出异常的评论更新#2

如果您有外键,则只能AUTO_INCREMENT通过常规SQL 重置:

$connection = $this->getEntityManager()->getConnection();
$connection->exec("ALTER TABLE <tablename> AUTO_INCREMENT = 1;");
Run Code Online (Sandbox Code Playgroud)

  • 引发异常:语法错误或访问冲突:1701无法截断外键约束中引用的表 (6认同)