Skip to content

After migration to a new project, querying relationships return error: "Could not find foreign keys between these entities. No relationship found between X and Y." #2200

Closed

Description

@EdgarBarrantes

Bug report

Describe the bug

Scenario: Using two projects (say: dev and staging). Both hosted by supabase. I'm migrating the schema like the recomendation, realtime disabled.

The issue: on dev environment, where I do everything by sql statements or interface I'm able to query relationships defined by foreign key. E.g.:

const { data, error } = await supabase
    .from<IYEntity>("X")
    .select(`name, uuid, Y (key, is_valid)`)
    .eq("uuid", id)
    .limit(1);

When using the staging project, I get "Could not find foreign keys between these entities. No relationship found between X and Y." while running the same query. I reviewed both DBs and they appear to be identical. I dropped all tables on the staging environment and deleted on procedures. The migration runs, everything appears to be fine, but the query still throws the same error.

To Reproduce

  1. Define a database with foreign key relationships. E.g.:
    image
  2. Migrate the DB to a new project.
  3. Try a query like the one shown.

System information

  • OS: Linux.
  • Version of supabase-js: 1.15.1
  • Version of Node.js: v15.11.0

Additional context

The only think I can think is of restarting the postgrest container, I'd love to be able to do that from the supabase interface or any API. Is there such an option?

Activity

  1. added
    bug
    on Jul 3, 2021
  2. steve-chavez commented on Jul 4, 2021

    @steve-chavez
    Member

    Hey @EdgarBarrantes,

    Try running this SQL and retry the postgrest-js request:

    -- manually refresh postgrest foreign key metadata
    NOTIFY ddl_command_end;

    It's likely that when you did the migration, the event trigger that automatically reloaded postgrest metadata was lost. This can be checked with:

    select evtname, evtevent from pg_event_trigger;
    -- should show
    -- api_restart ddl_command_end

    If the event trigger is not there, you can create it with:

    CREATE OR REPLACE FUNCTION extensions.notify_api_restart()
    RETURNS event_trigger
    LANGUAGE plpgsql
    AS $$
    BEGIN
        NOTIFY ddl_command_end;
    END;
    $$;
    CREATE EVENT TRIGGER api_restart ON ddl_command_end
    EXECUTE PROCEDURE extensions.notify_api_restart();
    COMMENT ON FUNCTION extensions.notify_api_restart IS 'Sends a notification to the API to restart. If your database schema has changed, this is required so that Supabase can rebuild the relationships.';

    Related to #1416 (comment)

  3. EdgarBarrantes commented on Jul 4, 2021

    @EdgarBarrantes
    Author

    Brilliant! Yeah that was it.

    Justed the migration again and indeed:

    ERROR:  permission denied to create event trigger "api_restart"
    HINT:  Must be superuser to create an event trigger.
    ERROR:  must be owner of event trigger api_restart
    

    I'll modify my migration script to run this after the schema is copied.

    Thank you so much!

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions