LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2005, 11:31 PM   #1
nazdrowie
Member
 
Registered: Oct 2004
Distribution: Debian
Posts: 39

Rep: Reputation: 15
PostgreSQL trigger dropping error


I'm trying to brush up on my postgresql skills, and I seem to be getting an unreasonable error when trying to drop what I know to be an existent trigger, which seems to beg the question whether I'm only brushing up on my postgresql skills If you'd like to follow along and see if you get the same error message, or help me find an answer, do the following to first set up the database so that you can use my .sql script.

Code:
$ createlang plpgsql template1 -Upostgres     # 'installs' the plpgsql language
$ dropdb sql4wn -Upostgres     # start with a fresh database for our experiment
$ createdb sql4wn -Upostgres
CREATE DATABASE
Now, here's the .sql script that's giving me problems. I've named it TriggerDroppingError.sql:
Code:
drop table phone_numbers;
drop table mailing_list;

create table mailing_list (
        email           varchar(100) not null primary key,
        name            varchar(100)
);

create table phone_numbers (
        email           varchar(100) not null references mailing_list(email),
        number_type     varchar(15) check (number_type in ('work','home','cell','beeper')),
        phone_number    varchar(20) not null
);

insert into mailing_list (name, email) values ('Philip Greenspun','philg@mit.edu');
insert into mailing_list (name, email) values ('Michael O''Grady','ogrady@fastbuck.com');
insert into phone_numbers values ('ogrady@fastbuck.com','work','(800) 555-1212');
insert into phone_numbers values ('ogrady@fastbuck.com','home','(617) 495-6000');
insert into phone_numbers values ('philg@mit.edu','work','(617) 253-8574');

alter table mailing_list add registration_date date;

-- WE DROP THE TRIGGER AND FUNCTION HERE,
-- EXPECTING TO GET ERRORS ONLY THE FIRST
-- TIME WE RUN THIS SCRIPT
drop trigger mailing_list_registration_date_trigger on mailing_list;
drop function mailing_list_registration_date();

-- HERE'S A FUNCTION THAT WE'LL USE IN THE TRIGGER
create function mailing_list_registration_date() returns trigger as $mailing_list_registration_date$
    begin
        if new.registration_date is null then
            new.registration_date := 'now';
        end if;
        return new;
    end;
$mailing_list_registration_date$ language plpgsql;

-- HERE'S THE TRIGGER
create trigger mailing_list_registration_date_trigger before insert or update on mailing_list
    for each row execute procedure mailing_list_registration_date();

-- Now we can test the trigger
insert into mailing_list (email, name) values ('testing@triggers.org', 'Trigger Tester');
-- We expect the row inserted above to not have a null date field in the output
-- from the below select command thanks to our function/trigger combo
select * from mailing_list;
When I run the script as per the first line of the following bash output, I get some expected errors when trying to drop tables and later the trigger and function, which don't yet exist, but when I run the script a second time, I shouldn't get these errors, and yet I get an error regarding the trigger, which reads 'ERROR: trigger "mailing_list_registration_date_trigger" for table "mailing_list" does not exist':

Code:
$ psql sql4wn -Upostgres < TriggerDroppingError.sql
ERROR:  table "phone_numbers" does not exist
ERROR:  table "mailing_list" does not exist
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "mailing_list_pkey" for table "mailing_list"
CREATE TABLE
CREATE TABLE
INSERT 18272 1
INSERT 18273 1
INSERT 18274 1
INSERT 18275 1
INSERT 18276 1
ALTER TABLE
ERROR:  trigger "mailing_list_registration_date_trigger" for table "mailing_list" does not exist
ERROR:  function mailing_list_registration_date() does not exist
CREATE FUNCTION
CREATE TRIGGER
INSERT 18279 1
        email         |       name       | registration_date
----------------------+------------------+-------------------
 philg@mit.edu        | Philip Greenspun |
 ogrady@fastbuck.com  | Michael O'Grady  |
 testing@triggers.org | Trigger Tester   | 2005-08-01
(3 rows)

$ psql sql4wn -Upostgres < TriggerDroppingError.sql
DROP TABLE
DROP TABLE
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "mailing_list_pkey" for table "mailing_list"
CREATE TABLE
CREATE TABLE
INSERT 18291 1
INSERT 18292 1
INSERT 18293 1
INSERT 18294 1
INSERT 18295 1
ALTER TABLE
ERROR:  trigger "mailing_list_registration_date_trigger" for table "mailing_list" does not exist
DROP FUNCTION
CREATE FUNCTION
CREATE TRIGGER
INSERT 18298 1
        email         |       name       | registration_date
----------------------+------------------+-------------------
 philg@mit.edu        | Philip Greenspun |
 ogrady@fastbuck.com  | Michael O'Grady  |
 testing@triggers.org | Trigger Tester   | 2005-08-01
(3 rows)

$
Why am I getting that error? And another thing, if I execute 'psql sql4wn -Upostgres' on the command line, logging into the database through the psql client, I can comfortably drop the trigger without any errors. What's the deal, people?

Code:
Welcome to psql 8.0.3, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

sql4wn=# drop trigger mailing_list_registration_date_trigger on mailing_list;
DROP TRIGGER
sql4wn=#

Last edited by nazdrowie; 08-01-2005 at 11:34 PM.
 
Old 08-02-2005, 06:07 AM   #2
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
The table is dropped. So the association of the trigger with the table has gone. Try changing your code to

Code:
drop trigger mailing_list_registration_date_trigger on mailing_list;
drop function mailing_list_registration_date();
drop table phone_numbers;
drop table mailing_list;
This should give errors on your expected lines.

HTH
 
Old 08-02-2005, 11:25 PM   #3
nazdrowie
Member
 
Registered: Oct 2004
Distribution: Debian
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks! By the way, do you know of any way to drop a table only if it exists? I think in MySQL there's a way to do it and it looks something like this: drop table if exists tbl_name.
 
Old 08-03-2005, 12:03 AM   #4
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
AFAIK there is no clause for checking if the table exists.

But you can go for some hacks like the one mentioned in the link
http://archives.postgresql.org/pgsql-sql/2003-02/msg00338.php
I have not tried the same, so please check it out.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
An error Occurred during the file system check. Dropping you to shell; the system wil aneikei Linux - Newbie 3 02-11-2010 07:38 PM
trigger 0.5.1.c mirto Slackware 2 09-08-2005 06:34 AM
trigger istallation problem landroni Linux - Games 0 04-29-2005 10:03 AM
postgresql -odbc & postgresql-jdbc installation kjsubbu Linux - Software 0 06-19-2003 02:50 AM
PostgreSQL trigger? playroll Linux - Software 4 03-29-2003 02:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration