突然错误:org.postgresql.util.PSQLException:错误:“ id”列中的空值违反了非空约束

时间:2019-05-15 19:44:30

标签: java database postgresql

所以,我有一种将地址数据插入数据库的方法。

    public Address(String street_name, Integer evidenceNumber, Integer describingNumber, Integer PSC, String city, String state) {
    this.streetName = new SimpleStringProperty(street_name);
    this.evidenceNumber = new SimpleIntegerProperty(evidenceNumber);
    this.describingNumber = new SimpleIntegerProperty(describingNumber);
    this.postcode = new SimpleIntegerProperty(PSC);
    this.city = new SimpleStringProperty(city);
    this.state = new SimpleStringProperty(state);
}

....

public boolean addAddress() throws SQLException {
    int controlDuplicate = -1;
    controlDuplicate = findAddress();
    if (controlDuplicate != -1) {
        return false;
    }
    if (this.describingNumber.get() < 0 || this.evidenceNumber.get() < 0 || "".equals(this.city.get()) || "".equals(this.state.get()) || this.postcode.get() < 0) {
        return false;
    }
    //connector.getConn().setAutoCommit(false);
    String addAddress = "INSERT INTO address(street_name, evidence_number, describing_number, postcodes, city, country)"
            + "VALUES('" + this.streetName.get() + "', " + this.evidenceNumber.get() + ", " + this.describingNumber.get() + ", " + this.postcode.get() + ", '" + this.city.get() + "', '" + this.state.get() + "')";
    Statement st = connector.getConn().createStatement();
    try {
        st.executeUpdate(addAddress);
    } catch (PSQLException ex) {
        System.err.println("Insert Address execution unsuccessful!");
        Logger.getLogger(Address.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    st.close();
    return true;
}

一切正常,直到突然之间,当我再次尝试执行此操作时,输出为:

org.postgresql.util.PSQLException:错误:列“ id”中的空值违反了非空约束

有人知道问题可能在哪里吗?在任何人提出疑问之前,“ id”都设置为“串行主键”,直到最近该方法才出现问题。

编辑:最重要的是,似乎所有数据库实体中的所有数据突然丢失。有人发生过这种事吗?

1 个答案:

答案 0 :(得分:0)

从pgAdmin或psql(使用\ d table_name)检查表定义,以检查'id'是否仍然是serial。并且PostgreSQL在内部为序列类型的列创建序列,例如对于您的“ address_id_seq”,请检查此序列是否存在并具有适当的授予权限。