Em 13 de julho de 2015 21:12, Cleiton Luiz Domazak <[email protected]
> escreveu:

> Boa noite.
>
> Não sei se alguém já precisou disso.
>
> Preciso fazer um UPDATE em várias tabelas em um campo chave, porém existe
> relacionamento entre elas via FK, e preciso gerar o script de UPDATE desses
> campos dinamicamente, pois não quero engessar o script, caso alguma tabela
> seja adicionada ou removida do banco, o script deve continuar funcionando.
> Ou se existir uma ferramenta que pelo menos monte essa hierarquia.
>
> Segue um exemplo:
>
> CREATE TABLE pai
> (
>   id bigint NOT NULL,
>   value character varying(255),
>   CONSTRAINT pai_pkey PRIMARY KEY (id)
> );
>
>
> CREATE TABLE filho
> (
>   id bigint NOT NULL,
>   id_pai bigint,integridade
>   CONSTRAINT filho_pkey PRIMARY KEY (id),
>   CONSTRAINT filho_id_pai_fkey FOREIGN KEY (id_pai)
>       REFERENCES pai (id) MATCH SIMPLE
>       ON UPDATE NO ACTION ON DELETE NO ACTION
> );
>
> CREATE TABLE neto
> (
>   id bigint NOT NULL,
>   id_filho bigint,
>   CONSTRAINT neto_pkey PRIMARY KEY (id),
>   CONSTRAINT neto_id_filho_fkey FOREIGN KEY (id_filho)
>       REFERENCES filho (id) MATCH SIMPLE
>       ON UPDATE NO ACTION ON DELETE NO ACTION
> );
>
>
> Para eu realizar o UPDATE na tabela "PAI" eu teria que primeiro fazer um
> UPDATE na tabela "FILHO" que por sua vez depende de um UPDATE na tabela
> "NETO" (sim, nome bizarro só pra identificar :D), e eu não tenho como
> garantir quantos níveis eu tenho.
>
> ​Troque suas constraints para MATCH SIMPLE ON UPDATE CASCADE ON DELETE
RESTRICT, assim quando o id da tabela pai for alterado, as tabelas que
fazem referencia a tabela pai também serão alterados.

[]s
Danilo​
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a