update or delete on table “dcim_device“ violates foreign key constraint

完整报错:

<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();
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星梦客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值