Foreign key that relates to itself

Hey there folks,
I currently have a situation where I want a record in one table to point to another row in the same table. In other words this table must have a foreign key that points to its own primary key.

For example, we have a table called Units - a unit being a collection of lessons. We want a unit to have two new fields called priorStudyUnit and futureStudyUnit which point to other units in the collection.
I’ve tried various ways of getting working but nothing seems to be working at the moment.
I’m currently trying the following:

Expected behavior

When I update a unit in the UI I should be able to alter the futureStudyUnit / priorStudyUnit field so it links to another unit record.

Actual behavior

Getting errors at the moment when I attempt to do this.

I can see the error is resulting because the request above is being made in the background and responding with 404.


Please provide any relevant information about your setup.

  • Package Version:
  • Express Version:
  • Sequelize Version:
  • Database Dialect: postgres
  • Database Version:
  • Project Name:

Hello @Mitchell_Lloyd

I’ll try to replicate your issue on a test project

I just replicated your configuration on a test project on my machine.

In my project, everything works well when I’m not using spaces in the “as” parameter.
Try with priorStudyUnit!

Hey there @romaing ,
I tried that - it is now working! Thankyou very much for your help :slight_smile: