in reply to SQL question: Insertion with FK constraint
my %property_id; sub property_id { my ($db, $property) = @_; my $pid; unless (defined($pid = $property_id{$property}) { ($pid) = $db->query(..., $property)->list; unless (defined($pid)) { $db->query("INSERT ...", $property); $pid = $db->last_insert_id(); } $property_id{$property} = $pid; } return $pid; }
When inserting a record that has a property FK, just call the subroutine for its value, and it will get created if necessary.
|
|---|