CPF502D: Referential constraint violation
CPF502D means Db2 for i refused to add or change a record because a referential constraint failed. The record's foreign key value has no matching key in the parent file, so the row would be an orphan.
Other names for this code
Also written as CPF502D error, MSGID CPF502D, Referential constraint violation on member, CPF502D foreign key, CPF502D RPG 01022.
What it means
A referential constraint links a dependent file to a parent file. An order line file, for example, can have a foreign key on the order number that points to the order header file. Once that constraint is in place, Db2 for i checks every insert and every change to the foreign key in the dependent file. The new value must match a key in the parent file.
CPF502D is the message the database sends when that check fails. Its first-level text says there was a referential constraint violation on the member. The second-level text, which you see with F1, names the constraint, the dependent file that was being changed and the parent file where no match was found. It is a notify message, so an application can handle it.
SQL reports the same failure as SQLCODE -530. Native record I/O gets CPF502D directly. ILE RPG sets file status 01022, and an RPG program with no error indicator, E extender or INFSR stops with inquiry message RNQ1022. ILE COBOL sets file status 9R.
A sibling message, CPF503A, has the same first-level text. Read the second-level text to see which rule and which direction failed.
Common causes
- A program added a dependent record before its parent existed, for example an order line for an order header that hasn't been written yet.
- A program changed the foreign key field to a value that isn't in the parent file, often a typing error or an old code that has since been removed.
- A data load or CPYF filled the dependent file before the parent file, or loaded only part of the parent data.
- The values look the same but don't match exactly, such as different leading zeros, case or trailing characters in a character key.
- Another job deleted the parent record a moment earlier.
How to find the problem
- In the job log (DSPJOBLOG), put the cursor on CPF502D and press F1. The help names the constraint, the dependent file and member, and the parent file. Note the record and the key value the program was writing.
- Show the constraint definition with
DSPFD FILE(ORDLIB/, or use WRKPFCST. The definition lists the foreign key fields and the parent key they must match. In SQL, QSYS2.SYSREFCST and QSYS2.SYSKEYCST hold the same details.ORDLINE) TYPE(*CST) - Look up the key in the parent file, for example with a SELECT against the parent table, to confirm it really is missing.
- If an RPG program stopped with RNQ1022, the message before it in the job log is the CPF502D that explains why.
How to fix it
- Write the parent record first, then the dependent records. In a load, copy parent files before dependent files.
- Correct the foreign key value so it matches an existing parent key.
- If a missing parent is a normal business case, make the foreign key field null-capable and leave it null until a parent exists. A foreign key with a null value isn't checked against the parent.
- Handle the error in the program. Use an error indicator or the E extender and check for status 01022 in RPG, or file status 9R in COBOL, and send the user a clear message.
- For a large one-time load, an administrator can disable the constraint with CHGPFCST and enable it again afterwards. The database then checks the whole file and flags rows that don't match, so plan time to clean them up.
What it looks like
Referential constraint violation on member ORDLINE.
IBM documentation
IBM Display Message Descriptions (DSPMSGD). Use the documentation for your installed release.
This link opens the relevant IBM reference library or lookup instructions. Match the complete code and your installed product release; it is not a code-specific review.
Read the installed message description:
DSPMSGD MSGID(CPF502D) MSGF(QSYS/QCPFMSG)
Also inspect the full job log and second-level message text, including substitution values.
Related codes
Questions
What is the difference between CPF502D and CPF503A?
Both report a referential constraint violation and share the same first-level text. CPF502D is the one tied to inserting or updating a dependent record whose foreign key has no parent, which SQL reports as SQLCODE -530. The second-level text of either message names the constraint and what was blocked.
Why does my RPG program stop with RNQ1022?
The write or update got a referential constraint error and the program had no error indicator, E extender or INFSR to handle it. RPG then asks the operator what to do with inquiry message RNQ1022.
How do I find which constraint failed?
Press F1 on CPF502D in the job log. The help text names the constraint, and DSPFD TYPE(*CST) on the dependent file shows its definition.