Hallo,
ich habe gerade ein seltsames Problem. In meiner DB gibt es mehrere identische Schemas, d.h. gleiche Tabellen etc. Nun habe ich eine pl_pgsql Funktion,
die einen Eintrag in eine Tabelle macht, diese tabelle hat einen Fk-Constraint auf eine andere im gleichen Schema.
BSP :
create schema a;
create table a.company (id integer primary key , name varchar (100) );
create table a.company_objects(id serial, company_id integer references a.company(id) , obj_class varchar(100));
create schema b;
create table b.company (id integer primary key , name varchar (100) );
create table b.company_objects(id serial, company_id integer references b.company(id) , obj_class varchar(100));
create or replace function public.xxx( coid integer)
returns integer as $$ DECLARE newid integer;
BEGIN
insert into company_objects(company_id, obj_class) values (coid, ‘TEST’) returning id into newid;
return newid;
END;
$$ language plpgsql;
set search_path to a;
insert into company values (123,‘A’);
set search_path to b;
insert into company values (987,‘B’);
set search_path to a, public;
select xxx(123);
set search_path to b, public;
select xxx(987);
drop schema a cascade;
drop schema b cascade;
drop function public.xxx(integer);
============
Wenn ich das ausführe passiert das:
CREATE SCHEMA
psql:/tmp/xxx.sql:2: HINWEIS: CREATE TABLE / PRIMARY KEY erstellt implizit einen Index »company_pkey« für Tabelle »company«
CREATE TABLE
psql:/tmp/xxx.sql:3: HINWEIS: CREATE TABLE erstellt implizit eine Sequenz »company_objects_id_seq« für die »serial«-Spalte »company_objects.id«
CREATE TABLE
CREATE SCHEMA
psql:/tmp/xxx.sql:6: HINWEIS: CREATE TABLE / PRIMARY KEY erstellt implizit einen Index »company_pkey« für Tabelle »company«
CREATE TABLE
psql:/tmp/xxx.sql:7: HINWEIS: CREATE TABLE erstellt implizit eine Sequenz »company_objects_id_seq« für die »serial«-Spalte »company_objects.id«
CREATE TABLE
CREATE FUNCTION
SET
INSERT 0 1
SET
INSERT 0 1
SET
xxx
1
(1 Zeile)
SET
psql:/tmp/xxx.sql:28: FEHLER: Einfügen oder Aktualisieren in Tabelle »company_objects« verletzt Fremdschlüssel-Constraint »company_objects_company_id_fkey«
DETAIL: Schlüssel (company_id)=(987) ist nicht in Tabelle »company« vorhanden.
KONTEXT: SQL-Anweisung »insert into company_objects(company_id, obj_class) values (coid, ‘TEST’) returning id«
PL/pgSQL function xxx(integer) line 3 at SQL-Anweisung
psql:/tmp/xxx.sql:31: HINWEIS: Löschvorgang löscht ebenfalls 2 weitere Objekte
DETAIL: Löschvorgang löscht ebenfalls Tabelle a.company
Löschvorgang löscht ebenfalls Tabelle a.company_objects
DROP SCHEMA
psql:/tmp/xxx.sql:32: HINWEIS: Löschvorgang löscht ebenfalls 2 weitere Objekte
DETAIL: Löschvorgang löscht ebenfalls Tabelle company
Löschvorgang löscht ebenfalls Tabelle company_objects
DROP SCHEMA
DROP FUNCTION
=======================
Welche Erklärung gibt es dafür ?
Der erste Funktionsaufruf in der Session geht immer gut, der zweite mit geändertem search_path schlägt fehl.
Grüße Rolf
P.S. Wollte das Skript hier Anhängen, aber sql , txt sei nicht erlaubt