在 h2 中重置自动增量 [英] Resetting autoincrement in h2

查看:36
本文介绍了在 h2 中重置自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个返回 json 响应的控制器,但测试第一次失败,因为h2数据库没有重置自动递增 ID.使用fixtures或手动创建对象也有同样的问题.

I'm testing a controller that returns a json response but the tests fail after the first time because the h2 database does not reset the auto increment id. Using fixtures or creating objects manually has a same problem.

@Before
public void setUp() {
    Fixtures.deleteAllModels();
    Fixtures.loadModels("data.yaml");
}

如何解决这个问题?

推荐答案

启动你的 play 应用程序,使用这个 url 启动浏览器(如果你在本地运行 play 应用程序):

Start your play app, fire up browser with this url (if you run play app locally):

http://localhost:9000/@db

输入您的 h2 db,然后键入以下命令并运行:

Enter your h2 db, and type the command below and run:

ALTER TABLE <table_name> ALTER COLUMN <column_name> RESTART WITH 1

如果您想以编程方式执行此操作,Fixtures.executeSQL() 可能会很有用

If you'd like to do this programmatically, Fixtures.executeSQL() might be useful

欲了解更多信息,请查看http://www.h2database.com/html/grammar.html#alter_table_alter

For more information, check http://www.h2database.com/html/grammar.html#alter_table_alter

这篇关于在 h2 中重置自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆