简体   繁体   中英

Sudden error: org.postgresql.util.PSQLException: ERROR: null value in column “id” violates not-null constraint

So, I have a method that inserts address data to the database.

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

Everything was working fine, until suddenly, when I tried to do it again, the output was:

org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint

Does anyone know where the problem could be? And before anyone questions, the "id" is set as Serial Primary Key and there was no problem with the method until recently.

EDIT: On top of that, it seems like all the data from all DB entities are suddenly lost. Have that happened to anyone?

Check table definition either from pgAdmin or psql(with \\d table_name) check if 'id' is still serial . And PostgreSQL internally creates sequence for column of type serial eg in your case 'address_id_seq' , please check if this sequence exists and has appropriate GRANTS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM