Retool DB foreign key

  1. My goal: add a foreign key to a column
  2. Issue: can'e select the primary key of the other table as a foreign key
  3. Steps I've taken to troubleshoot:
  4. Additional info: (Cloud or Self-hosted, Screenshots) - Cloud


can't select the table id I always select the table id field as a foreign key but today I found that the reference was broken, and the id can't be selected, it not appear as an option

1 Like

It happened the same to me today on the latest cloud version.

@Avner1 what I did was to log in on pgAdmin to the retool DB and manually create the foreign key there.

I'd suggest PGAdmin also, but if you're not a DB person and figuring out a whole new IDE sounds like overkill for this you could create a new Query and use the Retool Database resource type, then switch to SQL Mode and add:

BEGIN;

ALTER TABLE table_name
DROP CONSTRAINT constraint_fkey;

ALTER TABLE child_table
ADD CONSTRAINT constraint_name
FOREIGN KEY (fk_columns)
REFERENCES parent_table(parent_key_columns);
// you can also use the bellow line, just be sure to delete the semicolon above this line
// ON DELETE CASCADE;

COMMIT;

Then you can hit 'run' on the query and after it finishes running you can delete this Query, which I probably would incase someone would accidently run it or links it to a button or something.... I mean, obv unless you want to keep it to reuse for whatever reason :person_shrugging:

2 Likes