Skip to content

Declarative schema migration generation broken for JOIN views after CLI update #4027

Open

Description

@vanandtwocrows

Describe the bug
After upgrading to the latest Supabase CLI (2.34.3), supabase db diff in declarative schema mode is endlessly re-generating migrations for views that contain JOINs, even if no actual changes have been made to the view SQL.

Before the CLI update, these views were stable. Now, after every db diff, the same DROP VIEW / CREATE VIEW statements are produced, leading to an infinite migration loop.


To Reproduce
Steps to reproduce the behavior:

  1. Upgrade to the latest Supabase CLI
  2. Have a project using declarative schema (supabase/schemas/*.sql).
  3. Include a view with a join, for example:
create or replace view public.timeslips_view as
select
  o.name,
  tr.timeslips_requested,
  tr.created_at,
  tr.download_link,
  tr.modified_at
from public.timeslips_requests tr
left join public.organisations o on tr.organisation_id = o.id
order by tr.created_at desc;
  1. Create migration with supabase stop && supabase db diff -f migration_name
  2. Re-run the diff

Expected behavior
If no changes have been made to the database or declarative schema, supabase db diff should produce no output.


Actual behavior
The CLI repeatedly outputs the same DROP VIEW / CREATE VIEW statements for affected views. This repeats indefinitely after each run, even immediately after applying the migration.


Notes / Observations
• The issue appears specific to views containing JOINs — views without joins are unaffected.
• Happens whether or not schema qualification (public.) is used in the FROM clause.
• Removing WITH (security_invoker...) and ALTER ... OWNER from the declarative schema does not resolve it.
• Likely linked to a change in the CLI’s SQL deparser in the most recent release.

Activity

  1. nicksiscoe commented on Aug 20, 2025

    @nicksiscoe

    Noticing exact same behavior when upgrading to 2.34.3. If I revert to 2.34.0 this bug doesn't happen.

  2. lavisht22 commented on Oct 6, 2025

    @lavisht22

    Did you find any solutions to this? I don't have declarative schemas and this is still happening with me.

  3. nicksiscoe commented on Oct 8, 2025

    @nicksiscoe

    Did you find any solutions to this? I don't have declarative schemas and this is still happening with me.

    @lavisht22 Updating to latest supabase cli seems to work for me. Currently on 2.40.7

  4. YA9 commented on Nov 6, 2025

    @YA9

    Getting the same issue

  5. speedpro commented on Nov 7, 2025

    @speedpro

    I'm getting the same issue, but mine doesn't have a join:

    create or replace view "public"."active_items" as
    SELECT 
      item.id,
      item.name,
      ...
    FROM items WHERE (item.is_deactivated = false);

    It's odd. I had only one view which was stuck in this loop, but since switching to declarative schemas, I now have three views which are stuck in this loop.

  6. nicksiscoe commented on Nov 7, 2025

    @nicksiscoe

    This was briefly fixed for a few versions and has come back on the latest few versions... No issues on 2.40.7-ish but on latest it's broken again

  7. jperasmus commented on Nov 19, 2025

    @jperasmus

    Is there a known workaround for this for now? I think the limitation I am running into is because my views have security_invoker set, which I see is part of the known limitations. It recreates the views every time I run the diff.

  8. jdharrisnz commented on Nov 19, 2025

    @jdharrisnz

    @jperasmus Switching the diff tool with --use-pg-schema worked for me. The default migra hasn't been updated in years, and I read that Supabase will be changing the default to pg schema in the future. As always, closely check your migration files!

  9. jperasmus commented on Nov 20, 2025

    @jperasmus

    Thanks @jdharrisnz I tried pg-schema-diff now, which looked promising. Unfortunately, it looks like it doesn't currently support indexes on materialized views, so it is erroring out for me. stripe/pg-schema-diff#173

  10. chaintng commented on Nov 28, 2025

    @chaintng

    having same issue also

  11. paulteriele commented on Jan 19, 2026

    @paulteriele

    I'm experiencing the same issue while diffing our declarative schema against our migrations. Versions of Supabase CLI higher than v2.53.6 (the latest I've tested with is v2.72.7) will repeatedly (endlessly) result in migration file (with the exact same contents) for all views and materialized views that joins one or more times.

    This prevents us from upgrading our CLI forwards from v2.53.6.

    To Reproduce

    • Supabase v2.72.7 (or at least higher then v2.53.6, that version does still work)
    • Declarative Schema including one or more views with one or more joins
    • Matching migration file(s)
    • run $ supabase db diff -f changes (multiple times)

    System information

    • MacOS v26.2 (25C56)
    • Docker v4.57.0 (215387)
    • supabase/postgres-meta:v0.95.2
    • supabase/edge-runtime:v1.70.0
    • supabase/storage-api:v1.33.5
    • supabase/postgrest:v14.3
    • supabase/realtime:v2.69.2
    • supabase/mailpit:v1.22.3
    • supabase/gotrue:v2.185.0
    • supabase/kong:2.8.1
    • supabase/vector:0.28.1-alpine
    • supabase/logflare:1.28.0
    • supabase/postgres:17.6.1.071

    Result

    This will repeatedly (endlessly) result in migration file (with the exact same contents) for all views and materialized views that joins one or more times.

    Expected Behavior

    • No schema changes (especially after having ran $ supabase db diff -f changes once)

    Failed workarounds

    • $ supabase db diff --use-pg-schema -f changes
      • WARNING: --use-pg-schema flag is experimental and may not include all entities, such as views and grants.
      • Keeps resulting in:
      • failed to generate plan: generating plan statements: generating migration statements: resolving table diff: generating alter statements for "public"."redacted": generating sql: altering base table: resolving check constraints sql: generating alter statements for validate_redacted: generating sql: check constraints that depend on UDFs: not implemented
    • $ supabase db diff --use-pg-delta -f changes
      • Keeps resulting in postgres role changes and one specific function
    • supabase db diff --use-pgadmin -f changes
      • Keeps resulting in:
      • ⢿ Diffing local database with current migrations... error running container: exit 1

    Succesfull workaround

    Downgrading Supabase CLI to v2.53.6

  12. DropbackHQ commented on Feb 5, 2026

    @DropbackHQ

    This is still not resolved on latest 2.75.0 so we can't upgrade :(

  13. ChinaeduO commented on Mar 30, 2026

    @ChinaeduO

    Any updates on this, it is open for a long time and blocking us from updating.

  14. christopheroka commented on Apr 12, 2026

    @christopheroka

    Downgrading to 2.50.0 worked for me

  15. nicbyron commented on May 8, 2026

    @nicbyron

    @sweatybridge any updates on this? Haven't been able to update the CLI for a long time now...

  16. avallete commented on May 8, 2026

    @avallete
    Member

    @sweatybridge any updates on this? Haven't been able to update the CLI for a long time now...

    Hi there ! We've been working over pg-delta those past few months. We are looking for feedbacks:
    https://github.com/orgs/supabase/discussions/44938

  17. added
    do-not-close
    on Jun 17, 2026
  18. arnaud-aivision commented on Aug 1, 2026

    @arnaud-aivision

    Still reproducing on CLI 2.109.1 (Postgres 17.6), so this isn't limited to the 2.34.x era.

    Two data points that may narrow it, plus a consequence I don't think has been raised in this thread.

    It isn't only JOIN views

    Our affected view has no JOIN — the subscription lookup is a correlated scalar subquery:

    create or replace view public.user_account_workspace
      with (security_invoker = true) as
    select
        accounts.id, accounts.name, accounts.picture_url,
        (select status from public.subscriptions
          where account_id = accounts.id
          order by created_at desc limit 1) as subscription_status
    from public.accounts
    where primary_owner_user_id = (select auth.uid())
      and accounts.is_personal_account = true
    limit 1;

    An earlier revision of the same view used left join lateral (...) on true and also never converged, so both forms are affected.

    I confirmed programmatically that live pg_get_viewdef(..., false) and the definition the diff proposes are identical once whitespace and schema qualification are normalised — the diff is proposing to replace a definition with itself.

    I also tried to minimise it and failed: five synthetic views (correlated subquery; plain control; plain + security_invoker; a full structural clone with security_invoker + subquery + where + outer limit 1; and one with a user-defined enum output column) were each materialised via db diff -f + db reset, and all five converged. Only the real view keeps regenerating. That's consistent with the deparser theory in the original report rather than anything about the view's shape.

    The consequence: drop view also discards the view's privileges

    The generated statement is drop view if exists ... followed by create or replace view .... Neither reloptions nor privileges are re-emitted, and DROP VIEW discards both. So committing the generated output:

    1. drops security_invoker = true — the view then runs with the owner's privileges and RLS on the underlying tables no longer applies to it (in our schema that also bypasses a RESTRICTIVE policy gating a column behind MFA); and
    2. drops the view's grants, after which every read fails with:
    ERROR:  permission denied for view user_account_workspace
    

    We hit both by committing the generated statement, and caught (2) only because a pgTAP test failed. We now hand-append compensations to any migration that includes this recreate:

    ALTER VIEW public.user_account_workspace SET (security_invoker = true);
    GRANT SELECT ON public.user_account_workspace TO authenticated, service_role;

    The reloptions half is tracked in #3973 / #5476 / #792 / #3487, but I couldn't find the privileges half reported anywhere.

    Because the diff never converges, this statement reappears in every subsequent db diff -f — so it's a permanent trap rather than a one-off, and it's silent unless something happens to exercise the view under a non-owner role.

    Suggestion

    Even if the non-convergence is hard to pin down, having db diff preserve reloptions and privileges when it emits a view recreate would remove the sharp edge — the perpetual statement would at least be harmless.

    Happy to run experiments against a specific hypothesis if that would help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    do-not-closesupabase/cli🐛 Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions