完整报错:
<class 'django.db.utils.IntegrityError'>
update or delete on table "dcim_device" violates foreign key constraint "netbox_topology_view_device_id_436f3bfe_fk_dcim_devi" on table "netbox_topology_views_coordinate"
DETAIL: Key (id)=(35) is still referenced from table "netbox_topology_views_coordinate".
用下面脚本将pg库中所有表的id全部更新下就好了
public static void main(String[] args) {
// 配置HikariCP连接池
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://ip:5432/netbox");
config.setUsername("netbox");
config.setPassword("netbox");
config.setMaximumPoolSize(10); // 设置最大连接数,根据需求调整
config.setIdleTimeout(600000); // 设置空闲超时时间,单位毫秒
List<String> noidList = new ArrayList<>();
noidList.add("django_session");
noidList.add("extras_cachedvalue");
// 创建连接池数据源
try (HikariDataSource dataSource = new HikariDataSource(config)) {
// 从连接池获取数据库连接
try (Connection connection = dataSource.getConnection()) {
// 获取所有数据库表名称
Statement statement = connection.createStatement();
String showTablesQuery = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE';";
ResultSet resultSet = statement.executeQuery(showTablesQuery);
List<String> sqlList = new ArrayList<>();
List<String> tableList = new ArrayList<>();
while (resultSet.next()) {
String tableName = resultSet.getString("table_name");
tableList.add(tableName);
}
for (String table : tableList) {
if (!noidList.contains(table)) {
String sql = "SELECT pg_get_serial_sequence('" + table + "', 'id');";
System.out.println(sql);
ResultSet resultSet1 = statement.executeQuery(sql);
while (resultSet1.next()) {
String setval = resultSet1.getString("pg_get_serial_sequence");
String updateSequenceQuery = "SELECT setval('" + setval + "', (SELECT MAX(id) FROM public." + table + "));";
sqlList.add(updateSequenceQuery);
}
}
}
for (String item : sqlList) {
System.out.println("====sql:" + item);
ResultSet resultSet1 = statement.executeQuery(item);
while (resultSet1.next()) {
String setval = resultSet1.getString("setval");
System.out.println("返回值:" + setval);
}
}
System.out.println("=====================完成=========================");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}