dbTalk Databases Forums  

Re: Error dropping non-existent tables

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Re: Error dropping non-existent tables in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: Error dropping non-existent tables - 11-20-2004 , 10:51 AM






On Fri, Nov 19, 2004 at 21:38:33 -0800,
Baldeep Hira <baldeephira (AT) gmail (DOT) com> wrote:
Quote:
I am able to drop tables in PostgreSQL, but the problem arises when
the table does not exist and I try to execute a "drop table" command.
The simplest fix is to do the drop table outside of a transaction (so
that the error in the drop doesn't break the rest of your script).
If that won't work for you then you can write a custom function that
looks in the system catalog to see if the table exists before trying
the drop.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #2  
Old   
Baldeep Hira
 
Posts: n/a

Default Re: Error dropping non-existent tables - 11-20-2004 , 12:15 PM






Thanks Bruno. A silly followup question =)

On Sat, 20 Nov 2004 10:51:13 -0600, Bruno Wolff III <bruno (AT) wolff (DOT) to> wrote:
Quote:
On Fri, Nov 19, 2004 at 21:38:33 -0800,
Baldeep Hira <baldeephira (AT) gmail (DOT) com> wrote:

I am able to drop tables in PostgreSQL, but the problem arises when
the table does not exist and I try to execute a "drop table" command.

The simplest fix is to do the drop table outside of a transaction (so
that the error in the drop doesn't break the rest of your script).
If that won't work for you then you can write a custom function that
looks in the system catalog to see if the table exists before trying
the drop.

How do I move the "DROP TABLE" commands into a separate transaction? I
could move all the "DROP TABLE" commands into a separate sql-script
file, but then I never know which of the tables are existing in the
database, thus that script will break as well. When I execute a
bunch of SQL commands from a script file, do all of them form a single
transaction? Anyway, I can have multiple transactions from a single
script file?

Currently my sql-script file looks like this.

DROP TABLE table1 CASCADE;
DROP TABLE table2 CASCADE;
DROP TABLE table3 CASCADE;
DROP TABLE table4 CASCADE;

CREATE TABLE table1 (
name VARCHAR(255) NOT NULL,
id INTEGER NOT NULL
);

CREATE TABLE table2 (
id INTEGER NOT NULL,
type INTEGER NOT NULL
);

CREATE TABLE table3 (
id INTEGER NOT NULL,
p_id INTEGER NOT NULL,
CONSTRAINT fk_table3 FOREIGN KEY (id)
REFERENCES table2 (id)
);

CREATE TABLE table4 (
id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
CONSTRAINT pk_table4 PRIMARY KEY (id),
CONSTRAINT u_table4 UNIQUE (name)
);

thanks,
Baldeep.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #3  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: Error dropping non-existent tables - 11-20-2004 , 02:03 PM



On Sat, Nov 20, 2004 at 10:15:46 -0800,
Baldeep Hira <baldeephira (AT) gmail (DOT) com> wrote:
Quote:
How do I move the "DROP TABLE" commands into a separate transaction? I
could move all the "DROP TABLE" commands into a separate sql-script
file, but then I never know which of the tables are existing in the
database, thus that script will break as well. When I execute a
bunch of SQL commands from a script file, do all of them form a single
transaction? Anyway, I can have multiple transactions from a single
script file?
Just like you do below. You will get error messages for the failed drops,
but everything will still work correctly. Were you actually having a problem
other than seeing the error message?

Quote:
Currently my sql-script file looks like this.

DROP TABLE table1 CASCADE;
DROP TABLE table2 CASCADE;
DROP TABLE table3 CASCADE;
DROP TABLE table4 CASCADE;

CREATE TABLE table1 (
name VARCHAR(255) NOT NULL,
id INTEGER NOT NULL
);

CREATE TABLE table2 (
id INTEGER NOT NULL,
type INTEGER NOT NULL
);

CREATE TABLE table3 (
id INTEGER NOT NULL,
p_id INTEGER NOT NULL,
CONSTRAINT fk_table3 FOREIGN KEY (id)
REFERENCES table2 (id)
);

CREATE TABLE table4 (
id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
CONSTRAINT pk_table4 PRIMARY KEY (id),
CONSTRAINT u_table4 UNIQUE (name)
);
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.