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: