Detail record saving with negative FK - I know I'm doing something stupid

Argh, please help :frowning:

Scenario is a simple master/detail table pair. Both tables defined in my schema with correct relationship and update rules. Components added to my data module and correct master/detail relationship configured between them.

Iā€™m adding a new master record, which gets a temporary PK value of -1.
Then I add a new detail record, which gets a PK of -1 but also a FK ā€œparentidā€ value of -1, referencing the new master.
All fine at this point.

I call ApplyUpdates on the master table. Cascade updates are set.

Both records are added to the database so the cascade update is working fine.
Both recordsā€™ PK values in the database are generated automatically (MSSQL IDENTITY columns).

The problem is that the FK ā€œparentidā€ column value of the detail record in the database is still -1.
Basically the newly assigned PK of the master hasnā€™t been propagated down to the FK of the detail.

Now Iā€™ve got this type of hierarchy literally EVERYWHERE in my project and have never had a problem with it, just this particular pair of tables are causing me grief.

Iā€™m clearly missing something stupid somewhere but Iā€™m losing my mind trying to think what.

Any advice on anything else to check?

UPDATE: Ahh think Iā€™ve found it - think Iā€™m applying the detail before the master (update rules wrong). Why do I keep discovering the problem right after I post here?! :slight_smile:

That was my first thought as I was reading this ;). Glad you found it.

Iā€™m wondering if we could do anything to better detect/warn bad scenarios like this, @EvgenyK?

To be fair, if the underlying database is properly configured with a foreign key constraint on the detail table, it would throw an error when you tried to insert the detail record with the invalid FK value.

Whilst I do generally do this, unfortunately these two tables didnā€™t have this so there was no net to catch my stupidity :slight_smile:

Very true :wink: