isaacharrisholt / supawright

A Playwright test harness for E2E testing with Supabase
MIT License
46 stars 2 forks source link

user_id in table #6

Closed rohankm closed 7 months ago

rohankm commented 7 months ago

Hi, In my user_details table, the id is one to one relation with auth.users.id.

when i try to run the test

test('can create table with no dependencies', async ({ supawright }) => {
    await supawright.create('user_details')
  })

i get Error: Error inserting data into user_details: insert or update on table "user_details" violates foreign key constraint "user_details_id_fkey"

How do i fix this?

isaacharrisholt commented 7 months ago

Hey @rohankm. Can I see the table schema for user_details?

rohankm commented 7 months ago

Hey @rohankm. Can I see the table schema for user_details?

Hi, here is the schema

create table
  public.user_details (
    id uuid not null,
    created_at timestamp without time zone not null default now(),
    updated_at timestamp without time zone not null default now(),
    email text not null,
    photo_uri text null default ''::text,
    name text null default ''::text,
    constraint user_details_pkey primary key (id),
    constraint user_details_id_fkey foreign key (id) references auth.users (id) on delete cascade
  ) tablespace pg_default;
rohankm commented 7 months ago

basically, the id of the table is automatically filled from auth.uid() function

isaacharrisholt commented 7 months ago

Ahhhh if it has a default of auth.uid(), Supawright can't handle it because it uses the service account, which has a auth.uid() of null.

Take a look at #4 for a solution :)

rohankm commented 7 months ago

Thanks createUser works

btw does the trigger not work while creating user?

i have set up a trigger when a user sign ups it automatically creates a record in user_details

isaacharrisholt commented 7 months ago

Does it require any special fields in the metadata etc.? The trigger should still work if it's set up correctly - we use triggers on auth.users inserts too, and it works fine.

rohankm commented 7 months ago

no, it just takes the user ID and email and creates a row in user_details. When I manually sign up the trigger works

I tried to select and it returned no rows { data: [] }

isaacharrisholt commented 7 months ago

I can't see any reason it wouldn't work. Is the auth.users record being created successfully?

The createUser function just calls the Supabase sign up function under the hood:

  public async createUser(attributes?: AdminUserAttributes): Promise<User> {
    const { data, error } = await this.supabase().auth.admin.createUser({
      email: faker.internet.email(),
      password: faker.internet.password(),
      ...attributes
    })
    if (error) {
      log.error('Error creating user', { error, attributes })
      throw new Error('Error creating user: ' + error.message)
    }
    this.record({
      schema: 'auth',
      table: 'users',
      data: data.user as unknown as Select<Database, 'auth', 'users'>
    })
    return data.user
  }
isaacharrisholt commented 7 months ago

Can I see the code for your trigger?

isaacharrisholt commented 7 months ago

Closing due to inactivity