如何在PostgreSql上使用Jooq级联截断?

如何在PostgreSql上使用Jooq级联截断?,postgresql,jooq,truncate,cascade,Postgresql,Jooq,Truncate,Cascade,我正在编写集成测试,希望在每次测试后清理(postgres)数据库。因此,我假设对所有(实际上只有大部分)表执行级联截断操作是可行的 我正在开发一个使用Kotlin、Spring和Jooq的应用程序,这就是为什么我将截断器类与truncateCascade进行成像,我可以将其自动连接到SpringBootTest类中 import org.jooq.DSLContext import org.jooq.Table @Service class Truncator(private val dsl

我正在编写集成测试,希望在每次测试后清理(postgres)数据库。因此,我假设对所有(实际上只有大部分)表执行级联截断操作是可行的

我正在开发一个使用Kotlin、Spring和Jooq的应用程序,这就是为什么我将
截断器
类与
truncateCascade
进行成像,我可以将其自动连接到
SpringBootTest
类中

import org.jooq.DSLContext
import org.jooq.Table

@Service
class Truncator(private val dsl: DSLContext) {
    fun truncateCascade(tables: List<Table<*>>) {
        dsl.truncate ...
    }

//      single truncate work only for tables without foreign key constraints
//      so I can't simply iterate over all tables and call this method.
//    fun truncate(table: Table<*>) {
//        dsl.truncate(table).execute()
//    }
}
import org.jooq.DSLContext
导入org.jooq.Table
@服务
类截断器(专用值dsl:DSLContext){
趣味truncateCascade(表格:列表){
dsl.truncate。。。
}
//单截断仅适用于没有外键约束的表
//所以我不能简单地遍历所有的表并调用这个方法。
//趣味截断(表:表){
//truncate(表).execute()
//    }
}
基本上,我正在寻找
truncateCascade
的实现(假设这不是一种错误的方法)


在调查此问题时,我找到了关于Jooq的文档,并提到了continueIdentity或restartIdentity,但没有足够的Jooq或数据库经验将其整合在一起。

您缺少的是在
truncate()
命令中调用
cascade()

fun truncate(table: Table<*>) {
    dsl.truncate(table).cascade().execute()
}
fun截断(表:表){
truncate(table.cascade().execute())
}

另一种选择是完全删除您的模式,然后从头开始重新创建它。这对于测试来说可能更为健壮,对于中小型模式来说不需要花费太多时间。

一个级联的模式看起来非常奇怪。我不想删除单个表,而是同时删除多个表(因此,
vararg
现在改为
List
,它意味着一个表数组)!否则外键约束将阻止我。还有一些表我不想删除,因为这些表的内容几乎是静态的,所以我们不要删除模式。@StephanS:如果运行
truncate some\u table cascade
,它将自动截断引用
some\u table
的任何表。如果对每个表使用级联,则不必在单个TRUNCATE命令中列出所有表table@StephanS:嗯,我想你会写你在评论中提到的循环。:)你刚刚忘记(?)添加
cascade()
关键字。@horse\u没有名字,我认为(“自动截断引用某个表的任何表”)是拼图中缺少的部分!谢谢