question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to create foreign key on user auth table

See original GitHub issue

Bug report

Describe the bug

I’m trying to run a DB migration. When running the following query in the SQL editor on my supabase dashboard, it works with no issues:

CREATE TABLE "admins" (
  "id" bigserial PRIMARY KEY,
  "email" varchar,
  "first_name" varchar NOT NULL,
  "last_name" varchar NOT NULL,
  "created_at" timestamptz NOT NULL DEFAULT (now()),
  "updated_at" timestamptz NOT NULL DEFAULT (now())
);

ALTER TABLE "admins" ADD FOREIGN KEY ("email") REFERENCES "users" ("email");

When I run the same query via a migration i get the following error:

Error: pq: relation "users" does not exist

I began to look for the name of the users authentication table that supabase gives out of the box (love this) and was able to find this: https://github.com/supabase/supabase/blob/master/docker/dockerfiles/postgres/auth-schema.sql#L5

-- auth.users definition
CREATE TABLE auth.users (
...
}

So I tried replacing my foreign key entry to:

ALTER TABLE "admins" ADD FOREIGN KEY ("email") REFERENCES "auth.users" ("email");

But was still seeing the error. Is there a correct way to reference this table outside of the dashboard?

Another thought I had was I’m connecting to my database with the postgres user and password. Then i thought maybe the possible the postgres user doesn’t have access to the auth schema but saw the following line:

GRANT ALL PRIVILEGES ON SCHEMA auth TO postgres;

Any assistance here would be super appreciated!

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Use https://github.com/amacneil/dbmate as a db migration tool
  2. run dbmate new init to generate a new migration file
  3. Insert the query above to the migration file
  4. Run dbmate -u <supabase connection string> up
  5. See error

Expected behavior

The expected behavior is to see the migration run successfully. When taking out the foreign keys referencing the users auth table, the migration runs successfully and I’m able to see my table in my supabase dashboard.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: macOS

Additional context

N/A

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
w3b6x9commented, Aug 29, 2021

@kiyadotdev auth is not set in postgres role’s search_path, while it is set in the Supabase dashboard’s SQL editor role, so you’ll have to properly format the auth table ("auth.users" -> "auth"."users").

Try the following with the correct schema-qualified table format:

ALTER TABLE "admins" ADD FOREIGN KEY ("email") REFERENCES "auth"."users" ("email");
1reaction
w3b6x9commented, Aug 29, 2021

@kiyadotdev you got it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to add foreign key in Supabase - Stack Overflow
I am facing issue in Add Relationships Between Tables in Supabase Using Foreign Keys part. I have created a table called profile where...
Read more >
Troubleshoot Could not create Foreign Key errors during ...
This article guides through resolving the error "Could not create Foreign Key" when publishing OutSystems apps in Service Studio.
Read more >
Add users table with unique & foreign key constraints in ...
Then the alter table command to add foreign key constraint to the owner field: ALTER TABLE "accounts" ADD FOREIGN KEY ("owner") REFERENCES " ......
Read more >
Operating without foreign key constraints - PlanetScale
A foreign key is a logical association of rows between two tables, in a parent-child relationship. A row in a "parent" table may...
Read more >
Database Setup Error - The ALTER TABLE statement ...
Details: Database Error executing a statement. (ALTER TABLE DBO.USER_ACCOUNT ADD CONSTRAINT UA_USER_FK FOREIGN KEY (USERID) REFERENCES DBO.USERS ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found