How do I deal with foreign keys?

(tobygroves) #1


Wondering how I deal with foreign keys during updates.

I understand how Identity/AutoInc type fields work, assigning negative local values until updates are posted to the server, which then returns the actual IDs from the database engine.

I also get how this works automatically with master/detail hierarchies, so child records can use the temporary, negative values for their parent columns and this all works fine too.

I can’t figure out this scenario though:

I have a master table with two detail tables linked to it. Of the two detail tables, one can potentially link to a record in the other via a foreign key. What I want to do is add a record to the first detail table, then add a record to the second which refers to the record in the first. I then want to post both in the same update by calling ApplyUpdates on the master table.

This doesn’t work. Using the temporary negative ID from the first detail table as the foreign key value in the second detail table causes a violation as the framework obviously isn’t working its magic on this value.

Is there any way around this or am I going to have to apply updates after adding the first detail record to get the actual ID, then add the record to the second detail table?

(EvgenyK) #2

pls review Multi Level Details Sample : Multi Level (43.7 KB)

it does the same as you want. Use it with Relativity server.

Note: It’s from DA7 and may require some adapting to DA9.

Do you have defined FK between detail1 and detail2 in Schema Modeler?

(tobygroves) #3

Ah ok thanks. I don’t think I do have the FK defined in the schema - is that making all the difference?

(EvgenyK) #4

yes, it is required for correct update of data in m/d relations like your case: detail1 vs details2

(tobygroves) #5

Yep that works a treat, many thanks

(jasonli) #6

Hi EvgenyK,
This is help for me also,
but I have another 2 questions:
1.But how about the way for working on cocoa? for Master - detail update, I can’t find the documents or samples for it.
2.On Cocoa, how should we handle the AutoInc field and master detail, where we can get some document of cocoa master -detail solution ?
Thank you very much , I am sorry I am new on cocoa…

(marc hoffman) #8


I believe this should just work exactly the same way, on Cocoa.