Conditional Foreign Keys
rlasker3
Posts: 3 New member
Short: Does anyone know of how I would fill in a foreign key value only if the value of a different column is a certain value?
Long: I have an Equipment table. It has an Operator column that is a key reference to the Employee table. This column needs to be null if the Ownership type is set to Subcontracted. Pretty sure that this needs to be done with a Python script but I'm very new to this tool and am not sure of the best way to handle it.
Long: I have an Equipment table. It has an Operator column that is a key reference to the Employee table. This column needs to be null if the Ownership type is set to Subcontracted. Pretty sure that this needs to be done with a Python script but I'm very new to this tool and am not sure of the best way to handle it.
Tagged:
Best Answer
-
way0utwest Posts: 312 Rose Gold 1I don't see an easy way to do this. I know we could write a Python script that would read the table and optionally return a NULL, but that's an overly complex approach.
What I'd do is:- call SQL Data Generator from the command line, use a normal FK generator
- call a SQL script that looks at Equipment.Ownership and sets Equipment.Operator to null
Really this is likely a quicker approach. I wish SDG included some pre/post processing, but that's not likely to come soon. Instead, I'd set this as a known process for provisioning data sets. Call SDG, then call
update equipment
set operator = null
where ownershiptype = 'Subcontracted'
Editor, SQLServerCentral
Answers
Then, you want a NULL in this column if Operator.OwnershipType = Subcontracted, otherwise some valid FK. Is that correct?
Yes, this is correct. If the the equipment is subcontracted you do not assign an operator to it so it needs to be null. Otherwise, an operator can be assigned.
So are we saying that every time this situation occurs we have to 'park' the column update until after SDG completes and run a load of update scripts separate and outside of SDG?