PostgreSQL: How to create a foreign key on a table with inheritance - postgresql

I'm trying to create the following tables structure:
create table base_auditoria (
ip_usuario_aud character varying(30) not null,
id_usuarioa_aud integer not null
);
create table pessoa (
id serial not null,
nome character varying not null,
constraint pk__pessoa__id primary key (id)
)
inherits (base_auditoria);
create table pessoa_fisica (
cpf character varying(11) not null,
nome_mae character varying not null
)
inherits (pessoa);
create table usuario (
id serial not null,
id_pessoa integer not null,
email character varying(500) not null,
constraint pk_usuario_id primary key (id)
)
inherits (base_auditoria);
After creating them, insert only one record in the table "usuario" and only one record in the table "pessoa_fisica":
INSERT INTO pessoa_fisica (cpf, nome_mae, nome, ip_usuario_aud, id_usuario_aud)
VALUES ('cpf 001', 'nome mae 001', 'pessoa.nome 001', '255.255.255.255', 0);
INSERT INTO usuario (id_pessoa, email, ip_usuario_aud, id_usuario_aud)
VALUES (
(SELECT id FROM pessoa_fisica WHERE cpf = 'cpf 001' LIMIT 1),
'test#test', '0.0.0.0', 0
);
So I update the two records to contain the correct identifiers that are foreign keys:
UPDATE usuario SET id_usuario_aud = id;
UPDATE pessoa_fisica SET id_usuario_aud = (SELECT id FROM usuario LIMIT 1);
When I run the SELECT, I get the expected answers:
db=# SELECT * FROM usuario;
ip_usuario_aud | id_usuario_aud | id | id_pessoa | email
----------------+----------------+----+-----------+-----------
0.0.0.0 | 1 | 1 | 1 | test#test
(1 registro)
db=# SELECT * FROM pessoa_fisica
ip_usuario_aud | id_usuario_aud | id | nome | cpf | nome_mae
-----------------+----------------+----+-----------------+---------+--------------
255.255.255.255 | 1 | 1 | pessoa.nome 001 | cpf 001 | nome mae 001
(1 registro)
ab=# SELECT * FROM pessoa;
ip_usuario_aud | id_usuario_aud | id | nome
-----------------+----------------+----+-----------------
255.255.255.255 | 1 | 1 | pessoa.nome 001
(1 registro)
ab=# SELECT * FROM ONLY pessoa;
ip_usuario_aud | id_usuario_aud | id | nome
----------------+----------------+----+------
(0 registro)
When I try to add a FOREIGN KEY constraint with the following command, I get the error:
alter table usuario
add constraint fk_1
foreign key (id_pessoa)
references pessoa (id)
match simple on update no action on delete no action;
ERROR: insert or update on table "usuario" violates foreign key constraint
"fk_1" DETAIL: Key (id_pessoa) = (1) is not present in the table
"pessoa".
How can I solve this problem?
Solution 1: Remove inheritance and work with foreign key relationships only;
Solution 2: Create a trigger to copy the table data "pessoa_fisica" to "pessoa" with the command:
CREATE OR REPLACE FUNCTION fn_tg_1() RETURNS trigger AS
$BODY$
BEGIN
IF tg_op = 'INSERT' THEN
INSERT INTO pessoa
SELECT ip_usuario_aud, id_usuario_aud, id, nome
FROM pessoa_fisica
WHERE id = new.id;
ELSIF tg_op = 'UPDATE' THEN
UPDATE pessoa SET
ip_usuario_aud = new.ip_usuario_aud,
id_usuario_aud = new.id_usuario_aud,
nome = new.nome
WHERE id = new.id;
ELSIF tg_op = 'DELETE' THEN
DELETE FROM pessoa WHERE id = new.id;
END IF;
END
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
CREATE TRIGGER tg_1
BEFORE INSERT OR UPDATE OR DELETE
ON pessoa_fisica FOR EACH ROW
EXECUTE PROCEDURE fn_tg_1();
Thus the foreign key "fk_1" can be created.
Is there any other alternative?

Related

Duplicate key found, but nothing matches in the table

I have a function which receives some parameters, does a SELECT to see if a table row exists and returns FALSE if so. If not, it does an INSERT but currently always fails with a 'duplicate key'. Here's a pseudo-code version...
CREATE OR REPLACE FUNCTION bob_function (
p_work_id INTEGER,
p_location_id VARCHAR,
p_area_id VARCHAR,
p_scheduled_work_no INTEGER,
p_start_date_time TIMESTAMPTZ,
p_work_date_time TIMESTAMPTZ,
p_user_id INTEGER,
p_comments TEXT,
p_work_stat_code CHAR(1)
)
RETURNS BOOLEAN AS $$
BEGIN
IF EXISTS (
SELECT 1
FROM work_table
WHERE location_id = p_location_id
AND area_id = p_area_id
AND work_id = p_work_id
AND scheduled_work_no = p_scheduled_work_no
AND start_date_time = p_start_date_time
AND user_work_id = p_user_id
AND work_date_time = p_work_date_time
)
THEN
RAISE NOTICE 'Work already exists - SKIPPING';
RETURN FALSE;
END IF;
INSERT INTO work_table (
location_id,
area_id,
work_id,
scheduled_work_no,
start_date_time,
user_work_id,
work_date_time,
stat_code,
comment
)
VALUES (
p_location_id,
p_area_id,
p_work_id,
p_scheduled_work_no,
p_start_date_time,
p_user_id,
p_work_date_time,
v_work_stat_code,
p_comments
);
RETURN TRUE;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
The primary key is defined thus...
myDb=# \d task_work_pk
Index "schema1.task_work_pk"
Column | Type | Key? | Definition
-------------------+-----------------------------+------+-------------------
location_id | character varying(8) | yes | location_id
area_id | character varying(3) | yes | area_id
work_id | integer | yes | work_id
scheduled_work_no | integer | yes | scheduled_work_no
start_date_time | timestamp(0) with time zone | yes | start_date_time
user_work_id | integer | yes | user_work_id
work_date_time | timestamp(0) with time zone | yes | work_date_time
primary key, btree, for table "schema1.work_table"
Currently I get the following error every time I run this function...
ERROR: 23505: duplicate key value violates unique constraint "task_work_pk"
DETAIL: Key (location_id, area_id, work_id, scheduled_work_no, start_date_time, user_work_id, work_date_time)=(SITE_1, BOB, 218, 5, 2021-07-09 00:28:00+10, 1, 2021-07-09 21:00:15+10) already exists.
There are no rows whatsoever with work_id = 218 and this is the only place in the entire database where this table is written to. The function is only called no more frequently than once a minute and I'm 99% sure I've not got any race condition.
EDIT: updated to remove errors
I'm ignoring your PLPGSQL code because it is not real code and has obvious flaws.
Given that 218 doesn't exist the only way to cause that error without 218 pre-existing is to insert the same record twice in a single transaction.

PostgreSQL recursive parent/child query

I'm having some trouble working out the PostgreSQL documentation for recursive queries, and wonder if anyone might be able to offer a suggestion for the following.
Here's the data:
Table "public.subjects"
Column | Type | Collation | Nullable | Default
-------------------+-----------------------------+-----------+----------+--------------------------------------
id | bigint | | not null | nextval('subjects_id_seq'::regclass)
name | character varying | | |
Table "public.subject_associations"
Column | Type | Collation | Nullable | Default
------------+-----------------------------+-----------+----------+--------------------------------------------------
id | bigint | | not null | nextval('subject_associations_id_seq'::regclass)
parent_id | integer | | |
child_id | integer | | |
Here, a "subject" may have many parents and many children. Of course, at the top level a subject has no parents and at the bottom no children. For example:
parent_id | child_id
------------+------------
2 | 3
1 | 4
1 | 3
4 | 8
4 | 5
5 | 6
6 | 7
What I'm looking for is starting with a child_id to get all the ancestors, and with a parent_id, all the descendants. Therefore:
parent_id 1 -> children 3, 4, 5, 6, 7, 8
parent_id 2 -> children 3
child_id 3 -> parents 1, 2
child_id 4 -> parents 1
child_id 7 -> parents 6, 5, 4, 1
Though there seem to be a lot of examples of similar things about I'm having trouble making sense of them, so any suggestions I can try out would be welcome.
To get all children for subject 1, you can use
WITH RECURSIVE c AS (
SELECT 1 AS id
UNION ALL
SELECT sa.child_id
FROM subject_associations AS sa
JOIN c ON c.id = sa. parent_id
)
SELECT id FROM c;
CREATE OR REPLACE FUNCTION func_finddescendants(start_id integer)
RETURNS SETOF subject_associations
AS $$
DECLARE
BEGIN
RETURN QUERY
WITH RECURSIVE t
AS
(
SELECT *
FROM subject_associations sa
WHERE sa.id = start_id
UNION ALL
SELECT next.*
FROM t prev
JOIN subject_associations next ON (next.parentid = prev.id)
)
SELECT * FROM t;
END;
$$ LANGUAGE PLPGSQL;
Try this
--- Table
-- DROP SEQUENCE public.data_id_seq;
CREATE SEQUENCE "data_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE public.data_id_seq
OWNER TO postgres;
CREATE TABLE public.data
(
id integer NOT NULL DEFAULT nextval('data_id_seq'::regclass),
name character varying(50) NOT NULL,
label character varying(50) NOT NULL,
parent_id integer NOT NULL,
CONSTRAINT data_pkey PRIMARY KEY (id),
CONSTRAINT data_name_parent_id_unique UNIQUE (name, parent_id)
)
WITH (
OIDS=FALSE
);
INSERT INTO public.data(id, name, label, parent_id) VALUES (1,'animal','Animal',0);
INSERT INTO public.data(id, name, label, parent_id) VALUES (5,'birds','Birds',1);
INSERT INTO public.data(id, name, label, parent_id) VALUES (6,'fish','Fish',1);
INSERT INTO public.data(id, name, label, parent_id) VALUES (7,'parrot','Parrot',5);
INSERT INTO public.data(id, name, label, parent_id) VALUES (8,'barb','Barb',6);
--- Function
CREATE OR REPLACE FUNCTION public.get_all_children_of_parent(use_parent integer) RETURNS integer[] AS
$BODY$
DECLARE
process_parents INT4[] := ARRAY[ use_parent ];
children INT4[] := '{}';
new_children INT4[];
BEGIN
WHILE ( array_upper( process_parents, 1 ) IS NOT NULL ) LOOP
new_children := ARRAY( SELECT id FROM data WHERE parent_id = ANY( process_parents ) AND id <> ALL( children ) );
children := children || new_children;
process_parents := new_children;
END LOOP;
RETURN children;
END;
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
ALTER FUNCTION public.get_all_children_of_parent(integer) OWNER TO postgres
--- Test
SELECT * FROM data WHERE id = any(get_all_children_of_parent(1))
SELECT * FROM data WHERE id = any(get_all_children_of_parent(5))
SELECT * FROM data WHERE id = any(get_all_children_of_parent(6))

Column type inconsistency: character varying and character varying(30)

For my application, I created a SQL file patch to add a column user_name to an existant table :
CREATE OR REPLACE FUNCTION add_col(
_tbl VARCHAR, -- Table
_col VARCHAR, -- Column to add
_type regtype -- Type of that column
) RETURNS BOOL AS $$
BEGIN
-- Returns true if column has been added; false otherwise.
IF EXISTS (SELECT DISTINCT column_name
FROM information_schema.columns
WHERE table_schema LIKE current_schema and table_name LIKE _tbl and column_name LIKE _col
)
THEN
-- Column already exists in that table of that schema: do nothing.
RETURN false;
END IF;
-- Add column
EXECUTE 'ALTER TABLE ' || _tbl || ' ADD COLUMN ' || _col || ' ' || _type;
RETURN true;
END; $$ language 'plpgsql';
SELECT add_col('ack_event', 'user_name', 'VARCHAR(30)');
If I execute that and do a SELECT to display ack_event content, we see that the column user_name is added but with the "character varying" type:
| id | user_name |
| bigint | character varying |
|--------+-------------------+
| | |
However if the ack_event column is created directly with the user_name column, the type of user_name is "character_varying(30)" :
CREATE TABLE ACK_EVENT(
ID int8 not null,
USER_NAME VARCHAR(30),
CONSTRAINT PK_ACK_EVENT PRIMARY KEY (ID)
);
Result:
| id | user_name |
| bigint | character varying(30) |
|--------+-----------------------+
| | |
Why is there this inconsistency ? Is there a way to correct it and have character varying (30) in both cases ?
This may introduce some validation issues, but if you change the datatype of _type from regtype to a text datatype, I think it will input your DDL much more literally:
CREATE OR REPLACE FUNCTION add_col(
_tbl VARCHAR,
_col VARCHAR,
_type text -- this was previously regtype
) RETURNS varchar AS $$

Postgresql constraint where child table has date and foreign key to table that has date range such that parent's date range is valid for child's date

I'm using PostgreSQL 9.4. Let's say I have 2 tables:
CREATE TABLE parents(
id serial not null,
parent_daterange daterange not null,
constraint "parent_id" primary key ("id")
);
CREATE TABLE children(
id serial not null,
child_date date not null,
parent_id int4 not null references parents(id),
constraint "child_id" primary key ("id")
);
I want to make sure children.child_date is always inside of its respective parents.parent_daterange.
So, this would be valid:
select * from children;
id | child_date | parent_id
---+------------+-----------
1 | 2016-01-01 | 1
select * from parents;
id | parent_daterange
---+------------------
1 | [2015-12-31,2016-01-10]
This wouldn't be valid and in this case, I'd want the INSERT statement on the children table to fail:
select * from children;
id | child_date | parent_id
---+------------+-----------
1 | 2016-01-11 | 1
select * from parents;
id | parent_daterange
---+------------------
1 | [2015-12-31,2016-01-10]

postgresql 8.4 trigger/storedproc error : how to fix it?

An INSERT on a table triggers a stored proc where the following error occurs.
ERROR: column "targetedfamily" is of type boolean but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Where: PL/pgSQL function "fn_family_audit" line 19 at SQL statement
And here's the ERRING stored proc (notice that my attempt to fix the problem by doing CAST(NEW.targetedfamily AS BOOLEAN) does NOT seem to work)
CREATE OR REPLACE FUNCTION fn_family_audit() RETURNS TRIGGER AS $tr_family_audit$
BEGIN
--
-- Create a row in family_audit to reflect the operation performed on family,
-- make use of the special variable TG_OP to work out the operation.
--
IF (TG_OP = 'DELETE') THEN
INSERT INTO public.family_audit values (
DEFAULT, 'D', OLD.family_id, OLD.familyserialno, OLD.node_id, OLD.sourcetype, OLD.familyname,
OLD.familynamelocallang, OLD.hofname, OLD.hofnamelocallang, OLD.targetedfamily, OLD.homeless,
OLD.landless, OLD.dependentonlabour, OLD.womenprimaryearner, OLD.landlinenumber, OLD.username , now());
RETURN OLD;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO public.family_audit values(
DEFAULT, 'U',NEW.family_id, NEW.familyserialno, NEW.node_id, NEW.sourcetype, NEW.familyname,
NEW.familynamelocallang, NEW.hofname, NEW.hofnamelocallang, NEW.targetedfamily, NEW.homeless,
NEW.landless, NEW.dependentonlabour, NEW.womenprimaryearner, NEW.landlinenumber, NEW.username , now());
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO public.family_audit values(
DEFAULT, 'I',NEW.family_id, NEW.familyserialno, NEW.node_id, NEW.sourcetype, NEW.familyname,
NEW.familynamelocallang, NEW.hofname, NEW.hofnamelocallang, CAST(NEW.targetedfamily AS BOOLEAN), NEW.homeless,
NEW.landless, NEW.dependentonlabour, NEW.womenprimaryearner, NEW.landlinenumber, NEW.username , now());
RETURN NEW;
END IF;
RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$tr_family_audit$ LANGUAGE plpgsql;
Here's the table definition
nucleus4=# \d family;
Table "public.family"
Column | Type | Modifiers
---------------------+-----------------------------+------------------------------------------------------------
family_id | integer | not null default nextval('family_family_id_seq'::regclass)
familyserialno | integer | not null
sourcetype | character varying(20) | not null
familyname | character varying(100) |
familynamelocallang | character varying(255) |
hofname | character varying(100) | not null
hofnamelocallang | character varying(255) | not null
targetedfamily | boolean |
homeless | boolean |
landless | boolean |
dependentonlabour | boolean |
womenprimaryearner | boolean |
landlinenumber | character varying(20) |
username | character varying(20) | not null
adddate | timestamp without time zone | not null default now()
updatedate | timestamp without time zone | not null default now()
node_id | integer | not null
Indexes:
"PK_family" PRIMARY KEY, btree (family_id)
"family_idx" UNIQUE, btree (familyserialno, node_id)
Foreign-key constraints:
"family_fk" FOREIGN KEY (node_id) REFERENCES hierarchynode_master(node_id)
Referenced by:
TABLE "agriland" CONSTRAINT "FK_agriland_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "currentloans" CONSTRAINT "FK_currentloans_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "family_address" CONSTRAINT "FK_family_address_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "family_basic_info" CONSTRAINT "FK_family_basic_info_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "family_entitlement" CONSTRAINT "FK_family_entitlement_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "livestock" CONSTRAINT "FK_livestock_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "member" CONSTRAINT "FK_member_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "otherassets" CONSTRAINT "FK_otherassets_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT
Triggers:
tr_family_audit AFTER INSERT OR DELETE OR UPDATE ON family FOR EACH ROW EXECUTE PROCEDURE fn_family_audit()
tr_family_updatedate BEFORE UPDATE ON family FOR EACH ROW EXECUTE PROCEDURE fn_modify_updatedate_column()
nucleus4=#
Here's family_audit
nucleus4=# \d family_audit;
Table "public.family_audit"
Column | Type | Mod
---------------------+-----------------------------+----------------------------------
familyaudit_id | integer | not null default nextval('family_
operation | character(1) | not null
family_id | integer | not null
familyserialno | integer | not null
sourcetype | character varying(20) | not null
familyname | character varying(100) |
familynamelocallang | character varying(255) |
hofname | character varying(100) | not null
hofnamelocallang | character varying(255) | not null
targetedfamily | boolean |
homeless | boolean |
landless | boolean |
dependentonlabour | boolean |
womenprimaryearner | boolean |
landlinenumber | character varying(20) |
username | character varying(20) | not null
adddate | timestamp without time zone | not null default now()
node_id | integer | not null
Indexes:
"PK_family_audit" PRIMARY KEY, btree (familyaudit_id)
nucleus4=#
Here's the trigger
CREATE TRIGGER tr_family_audit
AFTER INSERT OR UPDATE OR DELETE ON public.family
FOR EACH ROW EXECUTE PROCEDURE fn_family_audit();
I would appreciate any hints.
Thank you,
BR,
~A
Your problem is here:
NEW.hofnamelocallang
Your insert has one extra column (apparently NEW.node_id). Try changing your insert to:
INSERT INTO public.family_audit values(
DEFAULT, 'I',NEW.family_id, NEW.familyserialno,
NEW.sourcetype, NEW.familyname,
NEW.familynamelocallang, NEW.hofname, NEW.hofnamelocallang,
NEW.targetedfamily, NEW.homeless,
NEW.landless, NEW.dependentonlabour, NEW.womenprimaryearner,
NEW.landlinenumber, NEW.username , now()
);
The error you are getting is basically saying that you were trying to insert NEW.hofnamelocallang into targetedfamily column (which is boolean, not varchar) because of the extra column you were putting in the insert sentence.
I would advice that, when you are performing an insert, for sanity reasons, always enumerate the columns you are putting values into. Something like this:
insert into table foo
(col1, col2, col3) -- column enumeration here
values
(1, 2, 3);