Unable to create foreign key on user auth table
See original GitHub issueBug 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:
- Use https://github.com/amacneil/dbmate as a db migration tool
- run
dbmate new init
to generate a new migration file - Insert the query above to the migration file
- Run
dbmate -u <supabase connection string> up
- 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:
- Created 2 years ago
- Comments:5 (5 by maintainers)
@kiyadotdev
auth
is not set inpostgres
role’ssearch_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:
@kiyadotdev you got it!