Hey everyone, @wyotim, this is your area of expertise isn't it?
My first time SQL app and am having a challenge:
How do I Patch a table that has a foreign key dependency on another table that has not been patched yet?
What am I missing?
Thank you
Solved! Go to Solution.
Hey buddy! I got it (following your lead of course:) )
Two of the tables (redacted, sorry) are for adding new items in the app, not really part of this Patch so I left them out.
For the other three tables, you were right @wyotim, it was all about the Patch order and grabbing the PK's after each Patch and slapping them into the FK of the following table. I used the Last() function for this.
Example:
//Patch values into dateTable Patch('[dbo].[dateTable]', Defaults('[dbo].[dateTable]'), { siteId: varSiteID, readingDate: Now() } ); //Patch values into readingTable Patch('[dbo].[readingTable]', Defaults( '[dbo].[readingTable]'), { dateId: Last('[dbo].[dateTable]').dateId, <--BINGO unitNum: 1, xzyName: 1, zyxNum: 1, xkdFactor: 1, supplyXya: 1, supplyUio: 1, sortNum: 1, currentUys: 1, avgJJk: 1, prevLLk: 1, readingNotes: "This is awesome" } ); //Patch values into the imageTable ForAll( colImageGallery, Patch( '[dbo].[imageTable]', Defaults('[dbo].[imageTable]'), { readingId: Last('[dbo].[readingTable]').readingId, <--BINGO photo: image, photoNotes: " " } ) );
Woohoo! That one was not simple to find info on.
Do I have to do this? https://docs.microsoft.com/en-us/sql/relational-databases/tables/disable-foreign-key-constraints-wit....
This kind of looks like disabling the FK altogether.
Thanks for the detailed response @wyotim. I just realized I have "CASCADE" set for each FK. I went in and set that at "NO". We'll see if that works.
RE: Why FK's and no PK's?:
This is my first SQL rodeo, so definitely not 100% solid on what I'm doing.
I wasn't too clear in my question. I was more wondering why you would have child records (by using an FK reference) but no parent records (the PK used as an FK in the child table).
To analogize, it is like the parent table is a record of transactions (like grocery receipts with customer names, date/times, store number, etc.) and the child table is a record of items within each transaction (milk, eggs, etc.). It seemed like you were adding items but didn't have a transaction to tie them to. Does that sound right or am I misunderstanding?
You have it correct. How do I patch all 5 tables at once so that the FK's work? I think thats my problem. Chicken and egg thing.
You will need to patch them in order of dependency I think. So siteTable before dateTable, dateTable before readingTable, etc. And you will need to have a way to grab the PK from the parent so the child can use it as an FK.
Hm. That feels tricky. How are other people patching multiple SQL tables that have FK dependencies? I see Paul O'Flaherty patching away on SQL YouTube videos. but no details on how to setup Looks like he's using SQL Views to display information. Not sure whether he is writing to the Views (or if that is even possible)
I deleted all FK dependencies for now to test patching individually. Maybe i'll bring in one at a time now and test your idea.
Thanks for the insights @wyotim, its always nice working with ya!
OOOOooohhh.... what do you think of this?
Conditions for Modifying Data in Partitioned Views
The value being inserted into the partitioning column should satisfy at least one of the underlying constraints; otherwise, the insert action will fail with a constraint violation.
Hey buddy! I got it (following your lead of course:) )
Two of the tables (redacted, sorry) are for adding new items in the app, not really part of this Patch so I left them out.
For the other three tables, you were right @wyotim, it was all about the Patch order and grabbing the PK's after each Patch and slapping them into the FK of the following table. I used the Last() function for this.
Example:
//Patch values into dateTable Patch('[dbo].[dateTable]', Defaults('[dbo].[dateTable]'), { siteId: varSiteID, readingDate: Now() } ); //Patch values into readingTable Patch('[dbo].[readingTable]', Defaults( '[dbo].[readingTable]'), { dateId: Last('[dbo].[dateTable]').dateId, <--BINGO unitNum: 1, xzyName: 1, zyxNum: 1, xkdFactor: 1, supplyXya: 1, supplyUio: 1, sortNum: 1, currentUys: 1, avgJJk: 1, prevLLk: 1, readingNotes: "This is awesome" } ); //Patch values into the imageTable ForAll( colImageGallery, Patch( '[dbo].[imageTable]', Defaults('[dbo].[imageTable]'), { readingId: Last('[dbo].[readingTable]').readingId, <--BINGO photo: image, photoNotes: " " } ) );
Woohoo! That one was not simple to find info on.