事务回滚时Yii2重置db ID

时间:2016-01-30 22:43:30

标签: transactions yii2 rollback

我注意到事务回滚,主键ID不断增长。是否有可能以某种方式重置它们(自动增量)?

2 个答案:

答案 0 :(得分:0)

我猜您在safeDown()方法中使用了delete()。如果您要删除表中的所有数据,则应使用truncateTable(),或者使用execute()编写自己的slq。

答案 1 :(得分:0)

尝试将AUTO_INCREMENT设置为1

public function down()
{
    $this->delete('{{%email}}', ['id' => [1, 2, 3, 4, 5, 6, 7, 8, 9,]]);
    $this->execute('ALTER TABLE {{%email}} AUTO_INCREMENT = 1');
    // When you insert any other (not NULL or 0) value into an AUTO_INCREMENT column,
    // the column is set to that value and the sequence is reset so that the
    // next automatically generated value follows sequentially from the largest column value.
}
相关问题