fit_dev
fit_dev

Reputation: 7

Parent key not found and I'm not sure why

This is the error that I am getting! This is when I try to update some values in my sql MY_TABLE table! Ignore spellings! integrity constraint from what I see elsewhere is that I am trying to input a value that is not present in the parent table! I am just not sure which value that is!

integrity constraint (MY.FK_MY_TABLE) violated - parent key not found
; nested exception is java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (MY.FK_MY_TABLE) violated - parent key not found
@Component
public class JdbcTemplates {

    private JdbcTemplate insertTemplate;

    @Value("${TS_ID}")
    private String someId;

    private static final String INSERT_QUERY = "INSERT INTO MY_TABLE (CFN_NO,CS_D,SOME_CONSTANT," +
            "A_NO,R_NO,B_NAME,O_ID,C_ID,A_TYPE,B_STATUS,R_PAGE,R_DATETIME," +
            "T_S_RESULTS,D_CNT,B_ACCT_NO,ACC_TYPE,A_F_ID,A_C_ID,AP_ID) " +
            "VALUES(?,?,?,?,?,?,?,?,?,?,?,SYS_DATE,?,?,?,?,?,?,?)";

    @Resource(name = "dataSource")
    private DataSource dataSource;

    @PostConstruct
    public void init() {

        this.insertTemplate = new JdbcTemplate(this.dataSource);
    }

    public void insertActivity(final String cnfNumber, final Act act){

        insertActivityTemplate.update(INSERT_QUERY, new PreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, cnfNumber);
                ps.setString(2, act.getCSId());
                ps.setString(3, DAOConstants.SOME_CONSTANT);
                ps.setString(4, act.getAccNumber());
                ps.setString(5, act.getRNumber());
                ps.setString(6, act.getBName());
                ps.setString(7, act.getOId());
                ps.setString(8, act.getCId());
                ps.setString(9, activity.getAType().getTCode());
                ps.setString(10, YesNo.NO.getSLabel());
                ps.setInt(11, act.getRPage());
                ps.setInt(12, act.getTResults());
                ps.setInt(13, act.getDDataCount());
                ps.setString(14, act.getBNumber());
                ps.setString(15, act.getAType());
                ps.setString(16, act.getAId());
                ps.setString(17, act.getAId());
                ps.setString(18, tSId);
            }

        });
    }
}

Upvotes: 0

Views: 1234

Answers (1)

Tad Harrison
Tad Harrison

Reputation: 1293

It looks like you have sorted it out in your code, but next time you run into this problem in an Oracle database you can find out the answer from the data dictionary.

Go to the error message:

ORA-02291: integrity constraint (MY.FK_MY_TABLE) violated - parent key not found

Get the constraint owner and name from within the parens, MY.FK_MY_TABLE and then look it up in the data dictionary.

Log into the database using the same credentials your app uses.

select * from all_cons_columns where owner = 'MY' and CONSTRAINT_NAME = 'FK_MY_TABLE'

This should show you the table name and column(s) that are causing your distress.

There is an all_constraints view as well, but that won't tell you the column name.

Upvotes: 1

Related Questions