pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Martin Edlman <edlman@fortech.cz>
To: pgsql-bugs@postgresql.org
Subject: BUG #5770: Foreign key violation after insert
Date: Thu, 25 Nov 2010 12:52:55 GMT
Message-ID: <201011251252.oAPCqtHw055992@wwwmaster.postgresql.org> (raw)


The following bug has been logged online:

Bug reference:      5770
Logged by:          Martin Edlman
Email address:      edlman@fortech.cz
PostgreSQL version: 9.0.1
Operating system:   Scientific Linux 5.5 (RHEL)
Description:        Foreign key violation after insert
Details: 

Hello,

I have two tables with RI/FK. There is a AFTER INSERT trigger on a master
table (mail_account) which inserts a record to a slave table (amavis_user).
But I get an error message 
ERROR:  insert or update on table "amavis_user" violates foreign key
constraint "amavis_user_email_fkey"
DETAIL:  Key (email)=('test@mail.com') is not present in table
"mail_account".

I encountered this problem during migration of the database from PgSQL 8.4
(where it works) to PgSQL 9.0.1.

I tried to set the FK constraint DEFERRABLE INITIALLY DEFERRED, I tried to
CREATE CONSTRAINT TRIGGER ... DEFERRABLE INITIALLY DEFERRED, I tried to SET
CONSTRAINTS tmp.amavis_user_email_fkey DEFERRED inside the trigger function
... all combinations - none of these helped.

Is it a bug or am I doing something wrong? It worked in 8.4 as I wrote.

The database migration is stuck on this. Please give me a hint or advice.

Regards, Martin E.

Here are the tables and trigger definitions:

-- trigger function
CREATE OR REPLACE FUNCTION tmp.mail_account_to_amavis_user() RETURNS trigger
AS
$BODY$
DECLARE
	prio INTEGER;
BEGIN
	IF NEW.username = 'alias' THEN
		prio := 3;
	ELSE
		prio := 6;
	END IF;

	RAISE NOTICE 'insert into tmp.amavis_user(id, email, priority, policy_id)
values (%, %, %, 1)',
	NEW.id, NEW.email, prio;

	SET CONSTRAINTS tmp.amavis_user_email_fkey DEFERRED;

	INSERT INTO tmp.amavis_user (id, email, priority, policy_id)
	VALUES (NEW.id, quote_literal(NEW.email), prio, 1);

	RETURN NEW;
  END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION tmp.mail_account_to_amavis_user() OWNER TO import;

-- mail account table
CREATE TABLE tmp.mail_account
(
  id serial NOT NULL,
  username character varying(50) NOT NULL,
  "password" character varying(50) NOT NULL,
  email character varying(255),
  uid integer DEFAULT 8,
  gid integer DEFAULT 11,
  home character varying(100),
  CONSTRAINT mail_account_pkey PRIMARY KEY (id),
  CONSTRAINT mail_account_email UNIQUE (email)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE tmp.mail_account OWNER TO import;

-- trigger to insert a record to amavis_user
CREATE CONSTRAINT TRIGGER amavis_user
  AFTER INSERT
  ON tmp.mail_account
  DEFERRABLE INITIALLY DEFERRED
  FOR EACH ROW
  EXECUTE PROCEDURE tmp.mail_account_to_amavis_user();

-- table amavis user
CREATE TABLE tmp.amavis_user
(
  id serial NOT NULL,
  priority integer NOT NULL DEFAULT 7,
  policy_id integer,
  email character varying(255) NOT NULL,
  CONSTRAINT amavis_user_pkey PRIMARY KEY (id),
  CONSTRAINT amavis_user_email_fkey FOREIGN KEY (email)
      REFERENCES tmp.mail_account (email) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
      DEFERRABLE INITIALLY DEFERRED
)
WITH (
  OIDS=FALSE
);

-- insert data to mail_account
insert into tmp.mail_account(username,password,email) values
('test','pwd','test@mail.com')

-- output
-- NOTICE:  insert into tmp.amavis_user(id, email, priority, policy_id)
values (15, test@mail.com, 6, 1)
-- ERROR:  insert or update on table "amavis_user" violates foreign key
constraint "amavis_user_email_fkey"
-- DETAIL:  Key (email)=('test@mail.com') is not present in table
"mail_account".



view thread (3+ messages)  latest in thread

Message-ID: <201011251252.oAPCqtHw055992@wwwmaster.postgresql.org>
Permalink:  ../201011251252.oAPCqtHw055992@wwwmaster.postgresql.org/
Also on:    postgresql.org/message-id/201011251252.oAPCqtHw055992@wwwmaster.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: edlman@fortech.cz
  Subject: Re: BUG #5770: Foreign key violation after insert
  In-Reply-To: <201011251252.oAPCqtHw055992@wwwmaster.postgresql.org>
  References: <201011251252.oAPCqtHw055992@wwwmaster.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox