-- Constraints (PK, FK, UNIQUE, CHECK)
-- Gerado automaticamente em 2026-05-11T16:53:50.935Z
-- Total: 369

ALTER TABLE ONLY public._db_migrations
    ADD CONSTRAINT _db_migrations_filename_key UNIQUE (filename);

ALTER TABLE ONLY public._db_migrations
    ADD CONSTRAINT _db_migrations_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.addon_credits
    ADD CONSTRAINT addon_credits_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.addon_products
    ADD CONSTRAINT addon_products_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.addon_products
    ADD CONSTRAINT addon_products_slug_key UNIQUE (slug);

ALTER TABLE ONLY public.addon_transactions
    ADD CONSTRAINT addon_transactions_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.agenda_bloqueios
    ADD CONSTRAINT agenda_bloqueios_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.agenda_configuracoes
    ADD CONSTRAINT agenda_configuracoes_pkey PRIMARY KEY (owner_id);

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_sem_sobreposicao EXCLUDE USING gist (owner_id WITH =, tstzrange(inicio_em, fim_em, '[)'::text) WITH &&);

ALTER TABLE ONLY public.agenda_excecoes
    ADD CONSTRAINT agenda_excecoes_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.agenda_online_slots
    ADD CONSTRAINT agenda_online_slots_owner_id_weekday_time_key UNIQUE (owner_id, weekday, "time");

ALTER TABLE ONLY public.agenda_online_slots
    ADD CONSTRAINT agenda_online_slots_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.agenda_regras_semanais
    ADD CONSTRAINT agenda_regras_semanais_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.agenda_regras_semanais
    ADD CONSTRAINT agenda_regras_semanais_unique UNIQUE (owner_id, dia_semana, hora_inicio, hora_fim, modalidade);

ALTER TABLE ONLY public.agenda_slots_bloqueados_semanais
    ADD CONSTRAINT agenda_slots_bloqueados_seman_owner_id_dia_semana_hora_inic_key UNIQUE (owner_id, dia_semana, hora_inicio);

ALTER TABLE ONLY public.agenda_slots_bloqueados_semanais
    ADD CONSTRAINT agenda_slots_bloqueados_semanais_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.agenda_slots_regras
    ADD CONSTRAINT agenda_slots_regras_owner_id_dia_semana_key UNIQUE (owner_id, dia_semana);

ALTER TABLE ONLY public.agenda_slots_regras
    ADD CONSTRAINT agenda_slots_regras_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.agendador_configuracoes
    ADD CONSTRAINT agendador_configuracoes_pkey PRIMARY KEY (owner_id);

ALTER TABLE ONLY public.agendador_solicitacoes
    ADD CONSTRAINT agendador_solicitacoes_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.audit_logs
    ADD CONSTRAINT audit_logs_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.billing_contracts
    ADD CONSTRAINT billing_contracts_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.commitment_services
    ADD CONSTRAINT commitment_services_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.commitment_time_logs
    ADD CONSTRAINT commitment_time_logs_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.company_profiles
    ADD CONSTRAINT company_profiles_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.company_profiles
    ADD CONSTRAINT company_profiles_tenant_id_key UNIQUE (tenant_id);

ALTER TABLE ONLY public.contact_email_types
    ADD CONSTRAINT contact_email_types_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.contact_emails
    ADD CONSTRAINT contact_emails_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.contact_phones
    ADD CONSTRAINT contact_phones_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.contact_types
    ADD CONSTRAINT contact_types_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_assignments
    ADD CONSTRAINT conversation_assignments_pkey PRIMARY KEY (tenant_id, thread_key);

ALTER TABLE ONLY public.conversation_autoreply_log
    ADD CONSTRAINT conversation_autoreply_log_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_autoreply_settings
    ADD CONSTRAINT conversation_autoreply_settings_pkey PRIMARY KEY (tenant_id);

ALTER TABLE ONLY public.conversation_bot_sessions
    ADD CONSTRAINT conversation_bot_sessions_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_bots
    ADD CONSTRAINT conversation_bots_pkey PRIMARY KEY (tenant_id);

ALTER TABLE ONLY public.conversation_messages
    ADD CONSTRAINT conversation_messages_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_notes
    ADD CONSTRAINT conversation_notes_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_optout_keywords
    ADD CONSTRAINT conversation_optout_keywords_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_optouts
    ADD CONSTRAINT conversation_optouts_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_sla_breaches
    ADD CONSTRAINT conversation_sla_breaches_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_sla_rules
    ADD CONSTRAINT conversation_sla_rules_pkey PRIMARY KEY (tenant_id);

ALTER TABLE ONLY public.conversation_tags
    ADD CONSTRAINT conversation_tags_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.conversation_thread_tags
    ADD CONSTRAINT conversation_thread_tags_pkey PRIMARY KEY (tenant_id, thread_key, tag_id);

ALTER TABLE ONLY public.determined_commitment_fields
    ADD CONSTRAINT determined_commitment_fields_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.determined_commitments
    ADD CONSTRAINT determined_commitments_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.determined_commitments
    ADD CONSTRAINT determined_commitments_tenant_native_key_uq UNIQUE (tenant_id, native_key);

ALTER TABLE ONLY public.dev_auditoria_items
    ADD CONSTRAINT dev_auditoria_items_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_comparison_competitor_status
    ADD CONSTRAINT dev_comparison_competitor_statu_comparison_id_competitor_id_key UNIQUE (comparison_id, competitor_id);

ALTER TABLE ONLY public.dev_comparison_competitor_status
    ADD CONSTRAINT dev_comparison_competitor_status_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_comparison_matrix
    ADD CONSTRAINT dev_comparison_matrix_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_competitor_features
    ADD CONSTRAINT dev_competitor_features_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_competitors
    ADD CONSTRAINT dev_competitors_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_competitors
    ADD CONSTRAINT dev_competitors_slug_key UNIQUE (slug);

ALTER TABLE ONLY public.dev_generation_log
    ADD CONSTRAINT dev_generation_log_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_roadmap_items
    ADD CONSTRAINT dev_roadmap_items_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_roadmap_phases
    ADD CONSTRAINT dev_roadmap_phases_numero_key UNIQUE (numero);

ALTER TABLE ONLY public.dev_roadmap_phases
    ADD CONSTRAINT dev_roadmap_phases_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_test_items
    ADD CONSTRAINT dev_test_items_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_user_credentials
    ADD CONSTRAINT dev_user_credentials_email_key UNIQUE (email);

ALTER TABLE ONLY public.dev_user_credentials
    ADD CONSTRAINT dev_user_credentials_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.dev_verificacoes_items
    ADD CONSTRAINT dev_verificacoes_items_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.document_access_logs
    ADD CONSTRAINT document_access_logs_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.document_generated
    ADD CONSTRAINT document_generated_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.document_share_links
    ADD CONSTRAINT document_share_links_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.document_signatures
    ADD CONSTRAINT document_signatures_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.document_templates
    ADD CONSTRAINT document_templates_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.documents
    ADD CONSTRAINT documents_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.document_share_links
    ADD CONSTRAINT dsl_token_unique UNIQUE (token);

ALTER TABLE ONLY public.email_layout_config
    ADD CONSTRAINT email_layout_config_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.email_layout_config
    ADD CONSTRAINT email_layout_config_tenant_id_key UNIQUE (tenant_id);

ALTER TABLE ONLY public.email_templates_global
    ADD CONSTRAINT email_templates_global_key_key UNIQUE (key);

ALTER TABLE ONLY public.email_templates_global
    ADD CONSTRAINT email_templates_global_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.email_templates_tenant
    ADD CONSTRAINT email_templates_tenant_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.email_templates_tenant
    ADD CONSTRAINT email_templates_tenant_tenant_id_owner_id_template_key_key UNIQUE (tenant_id, owner_id, template_key);

ALTER TABLE ONLY public.entitlements_invalidation
    ADD CONSTRAINT entitlements_invalidation_pkey PRIMARY KEY (owner_id);

ALTER TABLE ONLY public.features
    ADD CONSTRAINT features_key_key UNIQUE (key);

ALTER TABLE ONLY public.features
    ADD CONSTRAINT features_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.feriados
    ADD CONSTRAINT feriados_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.feriados
    ADD CONSTRAINT feriados_tenant_id_data_nome_key UNIQUE (tenant_id, data, nome);

ALTER TABLE ONLY public.financial_categories
    ADD CONSTRAINT financial_categories_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.financial_exceptions
    ADD CONSTRAINT financial_exceptions_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.financial_records
    ADD CONSTRAINT financial_records_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.global_notices
    ADD CONSTRAINT global_notices_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.insurance_plan_services
    ADD CONSTRAINT insurance_plan_services_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.insurance_plans
    ADD CONSTRAINT insurance_plans_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.login_carousel_slides
    ADD CONSTRAINT login_carousel_slides_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.math_challenges
    ADD CONSTRAINT math_challenges_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.medicos
    ADD CONSTRAINT medicos_crm_owner_unique UNIQUE NULLS NOT DISTINCT (owner_id, crm);

ALTER TABLE ONLY public.medicos
    ADD CONSTRAINT medicos_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.module_features
    ADD CONSTRAINT module_features_pkey PRIMARY KEY (module_id, feature_id);

ALTER TABLE ONLY public.modules
    ADD CONSTRAINT modules_key_key UNIQUE (key);

ALTER TABLE ONLY public.modules
    ADD CONSTRAINT modules_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notice_dismissals
    ADD CONSTRAINT notice_dismissals_notice_id_user_id_key UNIQUE (notice_id, user_id);

ALTER TABLE ONLY public.notice_dismissals
    ADD CONSTRAINT notice_dismissals_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_channels
    ADD CONSTRAINT notification_channels_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_logs
    ADD CONSTRAINT notification_logs_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_preferences
    ADD CONSTRAINT notification_preferences_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_queue
    ADD CONSTRAINT notification_queue_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_schedules
    ADD CONSTRAINT notification_schedules_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_templates
    ADD CONSTRAINT notification_templates_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notifications
    ADD CONSTRAINT notifications_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.owner_users
    ADD CONSTRAINT owner_users_pkey PRIMARY KEY (owner_id, user_id);

ALTER TABLE ONLY public.patient_contacts
    ADD CONSTRAINT patient_contacts_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_discounts
    ADD CONSTRAINT patient_discounts_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_group_patient
    ADD CONSTRAINT patient_group_patient_pkey PRIMARY KEY (patient_group_id, patient_id);

ALTER TABLE ONLY public.patient_groups
    ADD CONSTRAINT patient_groups_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_intake_requests
    ADD CONSTRAINT patient_intake_requests_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_invite_attempts
    ADD CONSTRAINT patient_invite_attempts_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_invites
    ADD CONSTRAINT patient_invites_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_invites
    ADD CONSTRAINT patient_invites_token_key UNIQUE (token);

ALTER TABLE ONLY public.patient_patient_tag
    ADD CONSTRAINT patient_patient_tag_pkey PRIMARY KEY (patient_id, tag_id);

ALTER TABLE ONLY public.patient_status_history
    ADD CONSTRAINT patient_status_history_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_support_contacts
    ADD CONSTRAINT patient_support_contacts_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_tags
    ADD CONSTRAINT patient_tags_owner_name_uniq UNIQUE (owner_id, nome);

ALTER TABLE ONLY public.patient_tags
    ADD CONSTRAINT patient_tags_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patient_timeline
    ADD CONSTRAINT patient_timeline_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.patients
    ADD CONSTRAINT patients_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.payment_settings
    ADD CONSTRAINT payment_settings_owner_id_key UNIQUE (owner_id);

ALTER TABLE ONLY public.payment_settings
    ADD CONSTRAINT payment_settings_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.plan_features
    ADD CONSTRAINT plan_features_pkey PRIMARY KEY (plan_id, feature_id);

ALTER TABLE ONLY public.plan_prices
    ADD CONSTRAINT plan_prices_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.plan_public_bullets
    ADD CONSTRAINT plan_public_bullets_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.plan_public
    ADD CONSTRAINT plan_public_pkey PRIMARY KEY (plan_id);

ALTER TABLE ONLY public.plans
    ADD CONSTRAINT plans_key_key UNIQUE (key);

ALTER TABLE ONLY public.plans
    ADD CONSTRAINT plans_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.professional_pricing
    ADD CONSTRAINT professional_pricing_owner_commitment_key UNIQUE (owner_id, determined_commitment_id);

ALTER TABLE ONLY public.professional_pricing
    ADD CONSTRAINT professional_pricing_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.profiles
    ADD CONSTRAINT profiles_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.public_submission_attempts
    ADD CONSTRAINT public_submission_attempts_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.recurrence_exceptions
    ADD CONSTRAINT recurrence_exceptions_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.recurrence_exceptions
    ADD CONSTRAINT recurrence_exceptions_unique UNIQUE (recurrence_id, original_date);

ALTER TABLE ONLY public.recurrence_rule_services
    ADD CONSTRAINT recurrence_rule_services_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.recurrence_rules
    ADD CONSTRAINT recurrence_rules_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.saas_admins
    ADD CONSTRAINT saas_admins_pkey PRIMARY KEY (user_id);

ALTER TABLE ONLY public.saas_doc_votos
    ADD CONSTRAINT saas_doc_votos_doc_id_user_id_key UNIQUE (doc_id, user_id);

ALTER TABLE ONLY public.saas_doc_votos
    ADD CONSTRAINT saas_doc_votos_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.saas_docs
    ADD CONSTRAINT saas_docs_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.saas_faq_itens
    ADD CONSTRAINT saas_faq_itens_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.saas_faq
    ADD CONSTRAINT saas_faq_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.saas_security_config
    ADD CONSTRAINT saas_security_config_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.saas_twilio_config
    ADD CONSTRAINT saas_twilio_config_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.services
    ADD CONSTRAINT services_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.session_reminder_logs
    ADD CONSTRAINT session_reminder_logs_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.session_reminder_settings
    ADD CONSTRAINT session_reminder_settings_pkey PRIMARY KEY (tenant_id);

ALTER TABLE ONLY public.submission_rate_limits
    ADD CONSTRAINT submission_rate_limits_pkey PRIMARY KEY (ip_hash, endpoint);

ALTER TABLE ONLY public.subscription_events
    ADD CONSTRAINT subscription_events_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.subscription_intents_personal
    ADD CONSTRAINT subscription_intents_personal_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.subscription_intents_legacy
    ADD CONSTRAINT subscription_intents_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.subscription_intents_tenant
    ADD CONSTRAINT subscription_intents_tenant_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.subscriptions
    ADD CONSTRAINT subscriptions_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.support_sessions
    ADD CONSTRAINT support_sessions_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.support_sessions
    ADD CONSTRAINT support_sessions_token_unique UNIQUE (token);

ALTER TABLE ONLY public.tenant_feature_exceptions_log
    ADD CONSTRAINT tenant_feature_exceptions_log_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.tenant_features
    ADD CONSTRAINT tenant_features_pkey PRIMARY KEY (tenant_id, feature_key);

ALTER TABLE ONLY public.tenant_invites
    ADD CONSTRAINT tenant_invites_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.tenant_members
    ADD CONSTRAINT tenant_members_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.tenant_members
    ADD CONSTRAINT tenant_members_tenant_id_user_id_key UNIQUE (tenant_id, user_id);

ALTER TABLE ONLY public.tenant_modules
    ADD CONSTRAINT tenant_modules_owner_id_module_id_key UNIQUE (owner_id, module_id);

ALTER TABLE ONLY public.tenant_modules
    ADD CONSTRAINT tenant_modules_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.tenants
    ADD CONSTRAINT tenants_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.therapist_payout_records
    ADD CONSTRAINT therapist_payout_records_pkey PRIMARY KEY (payout_id, financial_record_id);

ALTER TABLE ONLY public.therapist_payouts
    ADD CONSTRAINT therapist_payouts_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.twilio_subaccount_usage
    ADD CONSTRAINT twilio_subaccount_usage_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.addon_credits
    ADD CONSTRAINT uq_addon_credits_tenant_type UNIQUE (tenant_id, addon_type);

ALTER TABLE ONLY public.notification_channels
    ADD CONSTRAINT uq_channel_per_owner UNIQUE NULLS NOT DISTINCT (owner_id, channel, deleted_at);

ALTER TABLE ONLY public.notification_preferences
    ADD CONSTRAINT uq_notif_prefs_patient_owner UNIQUE NULLS NOT DISTINCT (owner_id, patient_id, deleted_at);

ALTER TABLE ONLY public.notification_queue
    ADD CONSTRAINT uq_notif_queue_idempotency UNIQUE (idempotency_key);

ALTER TABLE ONLY public.notification_schedules
    ADD CONSTRAINT uq_notif_schedule_owner UNIQUE NULLS NOT DISTINCT (owner_id, schedule_key, deleted_at);

ALTER TABLE ONLY public.notification_templates
    ADD CONSTRAINT uq_notif_template_key UNIQUE NULLS NOT DISTINCT (tenant_id, owner_id, key, deleted_at);

ALTER TABLE ONLY public.user_settings
    ADD CONSTRAINT user_settings_pkey PRIMARY KEY (user_id);

ALTER TABLE ONLY public.whatsapp_connection_incidents
    ADD CONSTRAINT whatsapp_connection_incidents_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.whatsapp_credit_packages
    ADD CONSTRAINT whatsapp_credit_packages_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.whatsapp_credit_purchases
    ADD CONSTRAINT whatsapp_credit_purchases_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.whatsapp_credits_balance
    ADD CONSTRAINT whatsapp_credits_balance_pkey PRIMARY KEY (tenant_id);

ALTER TABLE ONLY public.whatsapp_credits_transactions
    ADD CONSTRAINT whatsapp_credits_transactions_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.addon_credits
    ADD CONSTRAINT addon_credits_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id);

ALTER TABLE ONLY public.addon_credits
    ADD CONSTRAINT addon_credits_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id);

ALTER TABLE ONLY public.addon_transactions
    ADD CONSTRAINT addon_transactions_admin_user_id_fkey FOREIGN KEY (admin_user_id) REFERENCES auth.users(id);

ALTER TABLE ONLY public.addon_transactions
    ADD CONSTRAINT addon_transactions_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id);

ALTER TABLE ONLY public.addon_transactions
    ADD CONSTRAINT addon_transactions_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.addon_products(id);

ALTER TABLE ONLY public.addon_transactions
    ADD CONSTRAINT addon_transactions_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id);

ALTER TABLE ONLY public.agenda_bloqueios
    ADD CONSTRAINT agenda_bloqueios_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agenda_bloqueios
    ADD CONSTRAINT agenda_bloqueios_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.agenda_configuracoes
    ADD CONSTRAINT agenda_configuracoes_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_billing_contract_id_fkey FOREIGN KEY (billing_contract_id) REFERENCES public.billing_contracts(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_determined_commitment_fk FOREIGN KEY (determined_commitment_id) REFERENCES public.determined_commitments(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_insurance_plan_id_fkey FOREIGN KEY (insurance_plan_id) REFERENCES public.insurance_plans(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_insurance_plan_service_id_fkey FOREIGN KEY (insurance_plan_service_id) REFERENCES public.insurance_plan_services(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_recurrence_id_fkey FOREIGN KEY (recurrence_id) REFERENCES public.recurrence_rules(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.agenda_eventos
    ADD CONSTRAINT agenda_eventos_terapeuta_fk FOREIGN KEY (terapeuta_id) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.agenda_excecoes
    ADD CONSTRAINT agenda_excecoes_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agenda_online_slots
    ADD CONSTRAINT agenda_online_slots_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agenda_online_slots
    ADD CONSTRAINT agenda_online_slots_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agenda_regras_semanais
    ADD CONSTRAINT agenda_regras_semanais_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agenda_slots_bloqueados_semanais
    ADD CONSTRAINT agenda_slots_bloqueados_semanais_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agenda_slots_regras
    ADD CONSTRAINT agenda_slots_regras_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agendador_configuracoes
    ADD CONSTRAINT agendador_configuracoes_owner_fk FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agendador_configuracoes
    ADD CONSTRAINT agendador_configuracoes_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agendador_solicitacoes
    ADD CONSTRAINT agendador_sol_owner_fk FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.agendador_solicitacoes
    ADD CONSTRAINT agendador_sol_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.audit_logs
    ADD CONSTRAINT audit_logs_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.audit_logs
    ADD CONSTRAINT audit_logs_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.billing_contracts
    ADD CONSTRAINT billing_contracts_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.billing_contracts
    ADD CONSTRAINT billing_contracts_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.commitment_services
    ADD CONSTRAINT commitment_services_commitment_id_fkey FOREIGN KEY (commitment_id) REFERENCES public.agenda_eventos(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.commitment_services
    ADD CONSTRAINT commitment_services_service_id_fkey FOREIGN KEY (service_id) REFERENCES public.services(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.commitment_time_logs
    ADD CONSTRAINT commitment_time_logs_calendar_event_id_fkey FOREIGN KEY (calendar_event_id) REFERENCES public.agenda_eventos(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.commitment_time_logs
    ADD CONSTRAINT commitment_time_logs_commitment_id_fkey FOREIGN KEY (commitment_id) REFERENCES public.determined_commitments(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.commitment_time_logs
    ADD CONSTRAINT commitment_time_logs_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.company_profiles
    ADD CONSTRAINT company_profiles_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.contact_email_types
    ADD CONSTRAINT contact_email_types_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.contact_emails
    ADD CONSTRAINT contact_emails_contact_email_type_id_fkey FOREIGN KEY (contact_email_type_id) REFERENCES public.contact_email_types(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.contact_emails
    ADD CONSTRAINT contact_emails_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.contact_phones
    ADD CONSTRAINT contact_phones_contact_type_id_fkey FOREIGN KEY (contact_type_id) REFERENCES public.contact_types(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.contact_phones
    ADD CONSTRAINT contact_phones_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.contact_types
    ADD CONSTRAINT contact_types_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_assignments
    ADD CONSTRAINT conversation_assignments_assigned_by_fkey FOREIGN KEY (assigned_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_assignments
    ADD CONSTRAINT conversation_assignments_assigned_to_fkey FOREIGN KEY (assigned_to) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_assignments
    ADD CONSTRAINT conversation_assignments_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_assignments
    ADD CONSTRAINT conversation_assignments_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_autoreply_log
    ADD CONSTRAINT conversation_autoreply_log_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_autoreply_settings
    ADD CONSTRAINT conversation_autoreply_settings_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_bot_sessions
    ADD CONSTRAINT conversation_bot_sessions_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_bots
    ADD CONSTRAINT conversation_bots_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_messages
    ADD CONSTRAINT conversation_messages_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_messages
    ADD CONSTRAINT conversation_messages_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_notes
    ADD CONSTRAINT conversation_notes_created_by_fkey FOREIGN KEY (created_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_notes
    ADD CONSTRAINT conversation_notes_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_notes
    ADD CONSTRAINT conversation_notes_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_optout_keywords
    ADD CONSTRAINT conversation_optout_keywords_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_optouts
    ADD CONSTRAINT conversation_optouts_blocked_by_fkey FOREIGN KEY (blocked_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_optouts
    ADD CONSTRAINT conversation_optouts_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_optouts
    ADD CONSTRAINT conversation_optouts_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_sla_breaches
    ADD CONSTRAINT conversation_sla_breaches_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_sla_rules
    ADD CONSTRAINT conversation_sla_rules_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_tags
    ADD CONSTRAINT conversation_tags_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_thread_tags
    ADD CONSTRAINT conversation_thread_tags_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES public.conversation_tags(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.conversation_thread_tags
    ADD CONSTRAINT conversation_thread_tags_tagged_by_fkey FOREIGN KEY (tagged_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.conversation_thread_tags
    ADD CONSTRAINT conversation_thread_tags_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.determined_commitment_fields
    ADD CONSTRAINT determined_commitment_fields_commitment_id_fkey FOREIGN KEY (commitment_id) REFERENCES public.determined_commitments(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.determined_commitment_fields
    ADD CONSTRAINT determined_commitment_fields_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.determined_commitments
    ADD CONSTRAINT determined_commitments_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.dev_comparison_competitor_status
    ADD CONSTRAINT dev_comparison_competitor_status_comparison_id_fkey FOREIGN KEY (comparison_id) REFERENCES public.dev_comparison_matrix(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.dev_comparison_competitor_status
    ADD CONSTRAINT dev_comparison_competitor_status_competitor_id_fkey FOREIGN KEY (competitor_id) REFERENCES public.dev_competitors(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.dev_competitor_features
    ADD CONSTRAINT dev_competitor_features_competitor_id_fkey FOREIGN KEY (competitor_id) REFERENCES public.dev_competitors(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.dev_roadmap_items
    ADD CONSTRAINT dev_roadmap_items_phase_id_fkey FOREIGN KEY (phase_id) REFERENCES public.dev_roadmap_phases(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.dev_verificacoes_items
    ADD CONSTRAINT dev_verificacoes_items_auditoria_item_id_fkey FOREIGN KEY (auditoria_item_id) REFERENCES public.dev_auditoria_items(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.document_access_logs
    ADD CONSTRAINT document_access_logs_documento_id_fkey FOREIGN KEY (documento_id) REFERENCES public.documents(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.document_generated
    ADD CONSTRAINT document_generated_documento_id_fkey FOREIGN KEY (documento_id) REFERENCES public.documents(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.document_generated
    ADD CONSTRAINT document_generated_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.document_generated
    ADD CONSTRAINT document_generated_template_id_fkey FOREIGN KEY (template_id) REFERENCES public.document_templates(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.document_share_links
    ADD CONSTRAINT document_share_links_documento_id_fkey FOREIGN KEY (documento_id) REFERENCES public.documents(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.document_signatures
    ADD CONSTRAINT document_signatures_documento_id_fkey FOREIGN KEY (documento_id) REFERENCES public.documents(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.documents
    ADD CONSTRAINT documents_agenda_evento_id_fkey FOREIGN KEY (agenda_evento_id) REFERENCES public.agenda_eventos(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.documents
    ADD CONSTRAINT documents_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.email_layout_config
    ADD CONSTRAINT email_layout_config_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.email_templates_tenant
    ADD CONSTRAINT email_templates_tenant_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.email_templates_tenant
    ADD CONSTRAINT email_templates_tenant_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.feriados
    ADD CONSTRAINT feriados_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.feriados
    ADD CONSTRAINT feriados_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.financial_categories
    ADD CONSTRAINT financial_categories_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.financial_exceptions
    ADD CONSTRAINT financial_exceptions_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.financial_records
    ADD CONSTRAINT financial_records_agenda_evento_id_fkey FOREIGN KEY (agenda_evento_id) REFERENCES public.agenda_eventos(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.financial_records
    ADD CONSTRAINT financial_records_category_id_fkey FOREIGN KEY (category_id) REFERENCES public.financial_categories(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.financial_records
    ADD CONSTRAINT financial_records_insurance_plan_id_fkey FOREIGN KEY (insurance_plan_id) REFERENCES public.insurance_plans(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.financial_records
    ADD CONSTRAINT financial_records_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.financial_records
    ADD CONSTRAINT financial_records_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.financial_records
    ADD CONSTRAINT financial_records_user_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.global_notices
    ADD CONSTRAINT global_notices_created_by_fkey FOREIGN KEY (created_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.insurance_plan_services
    ADD CONSTRAINT insurance_plan_services_plan_fkey FOREIGN KEY (insurance_plan_id) REFERENCES public.insurance_plans(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.insurance_plans
    ADD CONSTRAINT insurance_plans_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.module_features
    ADD CONSTRAINT module_features_feature_id_fkey FOREIGN KEY (feature_id) REFERENCES public.features(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.module_features
    ADD CONSTRAINT module_features_module_id_fkey FOREIGN KEY (module_id) REFERENCES public.modules(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.notice_dismissals
    ADD CONSTRAINT notice_dismissals_notice_id_fkey FOREIGN KEY (notice_id) REFERENCES public.global_notices(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.notice_dismissals
    ADD CONSTRAINT notice_dismissals_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.notification_channels
    ADD CONSTRAINT notification_channels_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.notification_logs
    ADD CONSTRAINT notification_logs_queue_id_fkey FOREIGN KEY (queue_id) REFERENCES public.notification_queue(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.notification_schedules
    ADD CONSTRAINT notification_schedules_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.notification_templates
    ADD CONSTRAINT notification_templates_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.notifications
    ADD CONSTRAINT notifications_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_contacts
    ADD CONSTRAINT patient_contacts_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_contacts
    ADD CONSTRAINT patient_contacts_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_discounts
    ADD CONSTRAINT patient_discounts_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_discounts
    ADD CONSTRAINT patient_discounts_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_group_patient
    ADD CONSTRAINT patient_group_patient_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_group_patient
    ADD CONSTRAINT patient_group_patient_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_groups
    ADD CONSTRAINT patient_groups_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_intake_requests
    ADD CONSTRAINT patient_intake_requests_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_invites
    ADD CONSTRAINT patient_invites_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_patient_tag
    ADD CONSTRAINT patient_patient_tag_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES public.patient_tags(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_patient_tag
    ADD CONSTRAINT patient_patient_tag_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_status_history
    ADD CONSTRAINT patient_status_history_alterado_por_fkey FOREIGN KEY (alterado_por) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.patient_status_history
    ADD CONSTRAINT patient_status_history_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_status_history
    ADD CONSTRAINT patient_status_history_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_support_contacts
    ADD CONSTRAINT patient_support_contacts_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_tags
    ADD CONSTRAINT patient_tags_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_timeline
    ADD CONSTRAINT patient_timeline_gerado_por_fkey FOREIGN KEY (gerado_por) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.patient_timeline
    ADD CONSTRAINT patient_timeline_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_timeline
    ADD CONSTRAINT patient_timeline_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patients
    ADD CONSTRAINT patients_convenio_id_fkey FOREIGN KEY (convenio_id) REFERENCES public.insurance_plans(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.patients
    ADD CONSTRAINT patients_responsible_member_id_fkey FOREIGN KEY (responsible_member_id) REFERENCES public.tenant_members(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.patients
    ADD CONSTRAINT patients_risco_sinalizado_por_fkey FOREIGN KEY (risco_sinalizado_por) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.patients
    ADD CONSTRAINT patients_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patients
    ADD CONSTRAINT patients_therapist_member_id_fkey FOREIGN KEY (therapist_member_id) REFERENCES public.tenant_members(id);

ALTER TABLE ONLY public.patients
    ADD CONSTRAINT patients_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.payment_settings
    ADD CONSTRAINT payment_settings_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.payment_settings
    ADD CONSTRAINT payment_settings_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.plan_features
    ADD CONSTRAINT plan_features_feature_id_fkey FOREIGN KEY (feature_id) REFERENCES public.features(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.plan_features
    ADD CONSTRAINT plan_features_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.plan_prices
    ADD CONSTRAINT plan_prices_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.plan_public_bullets
    ADD CONSTRAINT plan_public_bullets_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.plan_public
    ADD CONSTRAINT plan_public_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_patient_tag
    ADD CONSTRAINT ppt_patient_fk FOREIGN KEY (patient_id) REFERENCES public.patients(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.patient_patient_tag
    ADD CONSTRAINT ppt_tag_fk FOREIGN KEY (tag_id) REFERENCES public.patient_tags(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.professional_pricing
    ADD CONSTRAINT professional_pricing_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.profiles
    ADD CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.profiles
    ADD CONSTRAINT profiles_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id);

ALTER TABLE ONLY public.recurrence_exceptions
    ADD CONSTRAINT recurrence_exceptions_agenda_evento_id_fkey FOREIGN KEY (agenda_evento_id) REFERENCES public.agenda_eventos(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.recurrence_exceptions
    ADD CONSTRAINT recurrence_exceptions_recurrence_id_fkey FOREIGN KEY (recurrence_id) REFERENCES public.recurrence_rules(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.recurrence_rule_services
    ADD CONSTRAINT recurrence_rule_services_rule_id_fkey FOREIGN KEY (rule_id) REFERENCES public.recurrence_rules(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.recurrence_rule_services
    ADD CONSTRAINT recurrence_rule_services_service_id_fkey FOREIGN KEY (service_id) REFERENCES public.services(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.recurrence_rules
    ADD CONSTRAINT recurrence_rules_insurance_plan_id_fkey FOREIGN KEY (insurance_plan_id) REFERENCES public.insurance_plans(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.recurrence_rules
    ADD CONSTRAINT recurrence_rules_insurance_plan_service_id_fkey FOREIGN KEY (insurance_plan_service_id) REFERENCES public.insurance_plan_services(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.saas_admins
    ADD CONSTRAINT saas_admins_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.saas_doc_votos
    ADD CONSTRAINT saas_doc_votos_doc_id_fkey FOREIGN KEY (doc_id) REFERENCES public.saas_docs(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.saas_doc_votos
    ADD CONSTRAINT saas_doc_votos_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.saas_faq_itens
    ADD CONSTRAINT saas_faq_itens_doc_id_fkey FOREIGN KEY (doc_id) REFERENCES public.saas_docs(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.services
    ADD CONSTRAINT services_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.session_reminder_logs
    ADD CONSTRAINT session_reminder_logs_conversation_message_id_fkey FOREIGN KEY (conversation_message_id) REFERENCES public.conversation_messages(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.session_reminder_logs
    ADD CONSTRAINT session_reminder_logs_event_id_fkey FOREIGN KEY (event_id) REFERENCES public.agenda_eventos(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.session_reminder_logs
    ADD CONSTRAINT session_reminder_logs_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.session_reminder_settings
    ADD CONSTRAINT session_reminder_settings_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.subscription_intents_personal
    ADD CONSTRAINT sint_personal_subscription_id_fkey FOREIGN KEY (subscription_id) REFERENCES public.subscriptions(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.subscription_intents_tenant
    ADD CONSTRAINT sint_tenant_subscription_id_fkey FOREIGN KEY (subscription_id) REFERENCES public.subscriptions(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.subscription_events
    ADD CONSTRAINT subscription_events_subscription_id_fkey FOREIGN KEY (subscription_id) REFERENCES public.subscriptions(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.subscription_intents_personal
    ADD CONSTRAINT subscription_intents_personal_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.subscription_intents_tenant
    ADD CONSTRAINT subscription_intents_tenant_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.subscription_intents_legacy
    ADD CONSTRAINT subscription_intents_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.subscriptions
    ADD CONSTRAINT subscriptions_owner_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.subscriptions
    ADD CONSTRAINT subscriptions_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.support_sessions
    ADD CONSTRAINT support_sessions_admin_fk FOREIGN KEY (admin_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.support_sessions
    ADD CONSTRAINT support_sessions_tenant_fk FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.tenant_features
    ADD CONSTRAINT tenant_features_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.tenant_invites
    ADD CONSTRAINT tenant_invites_accepted_by_fkey FOREIGN KEY (accepted_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.tenant_invites
    ADD CONSTRAINT tenant_invites_invited_by_fkey FOREIGN KEY (invited_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.tenant_invites
    ADD CONSTRAINT tenant_invites_revoked_by_fkey FOREIGN KEY (revoked_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.tenant_invites
    ADD CONSTRAINT tenant_invites_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.tenant_members
    ADD CONSTRAINT tenant_members_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.tenant_members
    ADD CONSTRAINT tenant_members_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.tenant_modules
    ADD CONSTRAINT tenant_modules_module_id_fkey FOREIGN KEY (module_id) REFERENCES public.modules(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.tenant_modules
    ADD CONSTRAINT tenant_modules_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.therapist_payout_records
    ADD CONSTRAINT therapist_payout_records_financial_record_id_fkey FOREIGN KEY (financial_record_id) REFERENCES public.financial_records(id) ON DELETE RESTRICT;

ALTER TABLE ONLY public.therapist_payout_records
    ADD CONSTRAINT therapist_payout_records_payout_id_fkey FOREIGN KEY (payout_id) REFERENCES public.therapist_payouts(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.therapist_payouts
    ADD CONSTRAINT therapist_payouts_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.therapist_payouts
    ADD CONSTRAINT therapist_payouts_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.twilio_subaccount_usage
    ADD CONSTRAINT twilio_subaccount_usage_channel_fk FOREIGN KEY (channel_id) REFERENCES public.notification_channels(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.user_settings
    ADD CONSTRAINT user_settings_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.whatsapp_connection_incidents
    ADD CONSTRAINT whatsapp_connection_incidents_channel_id_fkey FOREIGN KEY (channel_id) REFERENCES public.notification_channels(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.whatsapp_connection_incidents
    ADD CONSTRAINT whatsapp_connection_incidents_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.whatsapp_credit_purchases
    ADD CONSTRAINT whatsapp_credit_purchases_created_by_fkey FOREIGN KEY (created_by) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.whatsapp_credit_purchases
    ADD CONSTRAINT whatsapp_credit_purchases_package_id_fkey FOREIGN KEY (package_id) REFERENCES public.whatsapp_credit_packages(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.whatsapp_credit_purchases
    ADD CONSTRAINT whatsapp_credit_purchases_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.whatsapp_credits_balance
    ADD CONSTRAINT whatsapp_credits_balance_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.whatsapp_credits_transactions
    ADD CONSTRAINT whatsapp_credits_transactions_admin_id_fkey FOREIGN KEY (admin_id) REFERENCES auth.users(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.whatsapp_credits_transactions
    ADD CONSTRAINT whatsapp_credits_transactions_conversation_message_id_fkey FOREIGN KEY (conversation_message_id) REFERENCES public.conversation_messages(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.whatsapp_credits_transactions
    ADD CONSTRAINT whatsapp_credits_transactions_purchase_id_fkey FOREIGN KEY (purchase_id) REFERENCES public.whatsapp_credit_purchases(id) ON DELETE SET NULL;

ALTER TABLE ONLY public.whatsapp_credits_transactions
    ADD CONSTRAINT whatsapp_credits_transactions_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;
