I have a C# application that must insert one parent record, and at least four children records, in hierarchical order. IOW, a parent Contract applies to one or more locations, each location has one or more items, each item has one or more services, and each service has one or more requirements. The application first obtains a set of Oracle sequence numbers, one from each table sequence for each record. For whatever reason (legacy database) each record has not only its parent’s sequence number, but also the contract sequence number.
So, the code begins a transaction, inserts the parent with the parent-level sequence number, then tries to insert the location record — already populated with both the parent number as an FK, and its own table sequence number. However, I get an Oracle-02291 error that the FK is violated because the parent number can’t be found.
INSERT into Contracts (contract_sequence_number, ...) values (10437, ...);
INSERT into Locations (location_sequence_number, contract_sequence_number, ...)
values (23733, 10437, ...);
...
I am guessing this is because the parent has not been committed, and therefore is not available. I can’t commit the parent, however, if any of the child records fail, so a commit before the child insert is out.
I know that this is such a common scenario, the answer must be pre-noob. But, all the answers I’ve found, so far, imply that the parent sequence number is found “in the table” so as to satisfy the FK.
Any thoughts on how I fix this are greatly appreciated.
Randy
Thank you all for your input. Turned out (as I suspected) it was my bad. Turns out there are two very similar schemas, and the connection I was using has access to both. Both schemas have tables by the same name. For reasons not clear to me, the parent was inserting into one schmea, but the child was attempting to insert into the other schema. Of course it couldn’t resolve the PK/FK relationship!
Again, thanks.