supabase: Unable to create foreign key relationships in the Table Editor

Bug report

Describe the bug

Unable to create relationships between tables using the ‘Add foreign key’ function in the table editor. Go through the add foreign key flow as described below and receive Could not find foreign keys between these entities. No relationship found between 'table_name' and 'table_name_2

To Reproduce

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

  1. Got to a table, right click the column you wish to add a foreign key for
  2. Select ‘edit column’
  3. Select ‘Add foreign key relation’
  4. Select the table and column to make a foreign key for
  5. Click save

Expected behavior

You get feed back that a relationship has been created, the response in the payload shows a relationship exists and i’m able to query the tables using select as shown in the documentation

Screenshots

supabase_foreign_key

Screenshot 2021-01-23 at 15 43 25

System information

  • OS: MacOS
  • Browser (if applies) Chrome
  • Version of supabase-js: -
  • Version of Node.js: -

Additional context

Please reach out for more context, or to take a call if needed

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 31 (15 by maintainers)

Most upvoted comments

I think I’m still getting this error when building relationships via the GUI - just wanted to bump in case anyone else is also experiencing it

I faced issue when adding a foreign key, and yes make sure the types are the same to avoid this, also you might have to delete the column and try again because for some reason I wasn’t able to update the column type, but deleted the old, created new column with proper type I want to add a foreign key to and it worked. Hope it helps someone.

@juanzgc there are certain use cases whereby the columns which are part of a composite key are not individually UNIQUE though. Composite keys indicate that the selected group of columns function as a unique identifier for the row - they aren’t 2 separate primary key columns (a table can only have 1 primary key). [ref](Under 5.3.4. Primary Keys)

A use case I can think of for example is:

Say I want to store a collection of all repositories on Github, I’d have a table that has a composite key of (organization, repository), followed by other columns such as owner, number of stars, etc. In this case, organization doesn’t have to be UNIQUE. Example data:

organization repository num_stars
postgres postgres 8000
postgres pgadmin3 5000
postgres pgadmin4 4000
postgres pgagent 3000

So I’d think that we shouldn’t implicitly set all primary keys to be UNIQUE by default, by rather let the user decide so

Based on a glance through this thread, it sounds like the bug affecting creation of foreign key relations has been fixed. Closing this out to avoid mixing in unrelated issues.

The UI around Unique constraints seems lacking, and is being tracked in #770

Hey @geordidearns! Sorry for the delay in response - we’ve managed to find where was going wrong in the editor and are pushing out a fix soon! Should fix the inability to create a foreign key relation via the editor.

On a separate note though, i was trying to reproduce the bug by following your schema in the screen grab. Just note that the column that are you trying to create a reference to (users.uuid in your context) has to be a primary key column, otherwise you’ll run into a SQL error: There is no unique constraint matching given keys for referenced table "users"

I’m presuming you might have run into some issues setting the primary key column for your users table as uuid, like having to manually insert a uuid to create a row. There’s a workaround currently for that:

  • Create a new table while unchecking the “Include primary key”.
  • Once the table is created, create a column (the first column that you create for a table that has no primary key will have a checkbox “Is primary key” thats checked) and select uuid as the type.
  • You’ll notice that you there’ll be a default option of “Automatically generate UUID” in the default value field
  • Go ahead and create the column and you shouldn’t need to manually enter a UUID when creating a new row

It’s really not the best experience - definitely hoping to reorganize the side panel fields to make everything more clear and seamless (e.g. Things like allowing you to specify the default value as automatically generate at the table creation step)

image

image

@akiarostami ahh interesting - specifying a column to be GENERATED AS IDENTITY will automatically assign a unique number to each row under that column [ref].

This also implies that UUID types cannot be declared as GENERATED AS IDENTITY hence why the error message you saw identity column type must be smallint, integer, or bigint

I’m not too sure why you were able to run that query initially though, but if you’re looking to create a UUID column with an automatically generated value for each row, you can also try using the following expression uuid_generate_v4() as such: CREATE TABLE petitions ( id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, ... );

@joshenlim Would it also be possible to add the Is Unique attribute to the Edit Column modal as well.

We can see the attribute in the Create Column modal but not in the Edit Column modal. Thanks in advanced!

image

@joshenlim Thanks! It seems that the UI has changed as of last night. Great to see this, very exciting!

I look forward to continue testing your services and I hope to be as helpful as possible!

@akiarostami thanks for the catch! we’ll look into this one too, and apologies for the really vague error message, we’ll try to improve that so its much more clearer!

I tried to reproduce your issue too and have a tentative workaround for the UI if you need one: this error seems to be popping up when you’re specifically trying to update a column from a numerical type (int, float) to uuid. Two ways around this, either

  • Create a new pid column with the reference up front
  • Cast your existing pid column to text type first, then to uuid

Sorry about this! I know it’s not the best experience, but we’ll definitely continue to ship improvements and make things better for everyone 😃

This is not resolved for me. When I try to add a foreign key, I get this error:

Error: Cannot read property ‘accept-version’ of undefined

Screen shot is attached.

CleanShot 2021-09-13 at 09 05 14