Checking for Foreign Keys constraining a record?

Поиск
Список
Период
Сортировка
От Benjamin Smith
Тема Checking for Foreign Keys constraining a record?
Дата
Msg-id 200604271638.35366.lists@benjamindsmith.com
обсуждение исходный текст
Ответы Re: Checking for Foreign Keys constraining a record?  (Kenneth Downs <ken@secdat.com>)
Re: Checking for Foreign Keys constraining a record?  (David Fetter <david@fetter.org>)
Список pgsql-general
I have a customer table (very important) and have numerous fields in other
tables FK to the serial id of the customer table.

There's an option to delete a customer record, but it has to fail if any
records are linked to it (eg: invoices) in order to prevent the books from
getting scrambled.

I want to be able to determine in advance whether or not a record is
"deleteable" before displaying the button to delete the record. If it's not
deleteable, it should say so before the user hits the button.

But, the only way that I've been able to find out if the customer record is
deletable is to begin a transaction, try to delete it, check to see if it
worked, and then rollback the session.

This causes my error logger to log errors everytime somebody looks at a
customer record, and (I'm sure) is not very efficient.

Is there a way to ask the database: "Are there any FK constraints that would
prevent this record from being deleted?"

Thanks,

-Ben
--
"The best way to predict the future is to invent it."
- XEROX PARC slogan, circa 1978

В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Help with unpredictable use of indexes on large tables...
Следующее
От: Jerry Sievers
Дата:
Сообщение: Re: Checking for Foreign Keys constraining a record?