Eu tenho essas tabelas: station
e water_types
. O usuário poderá ter apenas uma estação, e caso queira deletar o station
, o correspondente water_types
também deverá ser deletado. Em uma estação, pode haver vários arquivos water_types
.
create table
public.water_type (
id uuid not null default uuid_generate_v4 (),
name text not null,
price numeric(10, 2) not null,
user_id uuid not null,
constraint water_type_pkey primary key (id),
constraint water_type_user_id_fkey foreign key (user_id) references auth.users (id) on update cascade on delete restrict
) tablespace pg_default;
create table
public.station (
id uuid not null default gen_random_uuid (),
created_at timestamp with time zone null,
user_id uuid not null default auth.uid (),
..other data...
tel_no numeric null,
delivery_mode text null,
constraint station_pkey primary key (id),
constraint station_user_id_key unique (user_id),
constraint tation_user_id_fkey foreign key (user_id) references auth.users (id) on update cascade on delete cascade
) tablespace pg_default;
Com base na tabela, é assim que planejei implementar:
Planejei comparar o user_id
que o station
tem com o user_id
do water_types
. E se corresponder, excluirá todos os water_types
arquivos correspondentes user_id
.
Existe alguma outra abordagem melhor ou isso já seria suficiente?
Você pode adicionar uma restrição de chave estrangeira para
water_types
fazer o que quiser: