In PostgreSQL version 9.1, I have two tables: ICD9, and Dx. I now wish to update the parent table, ICD9, by changing an existing record to a different key value (in cdesc).
If the new key (cicd9,cdesc) does not already exist in the ICD9 table, then the new values are cascaded to the child table, Dx :-)
However, if the "new" key with the new value of cdesc already exists in the ICD9 table, then the record is not updated due to
error 23505: duplicate key value violates unique constraint "constraint_cdesc".
What I need to happen is that the update of the parent table, ICD9, updates the old record to the new values and this update is cascaded to all the children records in DX that used the old key and then to remove the now unused "old" record from the ICD9 table.
Any help for this newbie would be much appreciated. TIA
CREATE TABLE icd9
(
cicd9 character varying(8),
cdesc character varying(80) NOT NULL,
CONSTRAINT constraint_cdesc UNIQUE (cicd9, cdesc),
CONSTRAINT desccheck CHECK (cdesc::text <> ''::text)
)
CREATE TABLE dx
(
cicd9 character varying(8),
cdesc character varying(80) NOT NULL,
CONSTRAINT fk_icd9 FOREIGN KEY (cicd9, cdesc)
REFERENCES icd9 (cicd9, cdesc) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED
)
Edit #1: I guess I simplified these table structures to much in order to clarify my point, here are the full structures. Any help with this is much appreciated.
CREATE TABLE dx
(
recid serial NOT NULL,
cpatient character varying(33) NOT NULL,
cicd9 character varying(8),
cdesc character varying(80) NOT NULL,
tposted timestamp without time zone NOT NULL,
"timestamp" timestamp without time zone DEFAULT now(),
modified timestamp without time zone DEFAULT now(),
resolved boolean DEFAULT false,
treated boolean DEFAULT false,
chronic boolean DEFAULT false,
groupid character varying(33) NOT NULL,
service integer DEFAULT 0,
pmh boolean DEFAULT false,
explanation text,
CONSTRAINT pk_dx_recid PRIMARY KEY (recid),
CONSTRAINT dx_cpatient_fkey FOREIGN KEY (cpatient)
REFERENCES patients (cpatient) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT dx_groupid_fkey FOREIGN KEY (groupid)
REFERENCES charts (groupid) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT fk_icd9 FOREIGN KEY (cicd9, cdesc)
REFERENCES icd9 (cicd9, cdesc) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT noduplicate_dx UNIQUE (cicd9, cdesc, groupid, tposted),
CONSTRAINT desccheck CHECK (cdesc::text <> ''::text),
CONSTRAINT groupcheck CHECK (groupid::bpchar <> ''::bpchar),
CONSTRAINT patientcheck CHECK (cpatient::bpchar <> ''::bpchar)
)
WITH (
OIDS=FALSE
);
ALTER TABLE dx
OWNER TO postgres;
CREATE TABLE icd9
(
recid serial NOT NULL,
cicd9 character varying(8),
cdesc character varying(80) NOT NULL,
"timestamp" timestamp without time zone DEFAULT now(),
modified timestamp without time zone DEFAULT now(),
chronic boolean NOT NULL DEFAULT false,
common boolean NOT NULL DEFAULT false,
CONSTRAINT pk_icd9_recid PRIMARY KEY (recid),
CONSTRAINT constraint_cdesc UNIQUE (cicd9, cdesc),
CONSTRAINT desccheck CHECK (cdesc::text <> ''::text)
)
WITH (
OIDS=FALSE
);
ALTER TABLE icd9
OWNER TO postgres;
Aside from probable errors in table design, I could find no way of temporarily suspending the "unique_violation" which comes from updating a record's key values to values held by another record. In the design above, the ICD9 file is intended as a library where the records may or may not be used elsewhere in the system.
The solution to this question is to move child records from the tables referencing the library, and then delete the record which would cause the conflict. Much of this code comes from Finding Foreign Keys with No Indexes and a special thanks to @Patrick for providing the final key I needed to make this work. Below is the code (which works for me) to perform this work. Hope it helps somebody.