#2608Requested

Feature request: Foreign key constraints

Requested on GitHub by jdetter
Energy Committed0TeV

This has been asked for several times but most recently in discord: https://discord.com/channels/1037340874172014652/1352309930010480791/1352309991088062569

We need a way to have a column in one table reference a column in another table, something like:

#[spacetimedb(table, name = "my_table", )]
pub struct MyTable {
  #[primary_key]
  #[auto_inc]
  pub id: u64,
  // ...
}

#[spacetimedb(table, name = "another_table")]
pub struct AnotherTable {
  // WARNING: This is pseudocode and does not actually work.
  #[foreign_key(MyTable)]
  pub id: u64,
  // ...
}

The user I referenced above has this specific case where they have a Vec<u64> which each one of those u64s references a key in another table. So it's more like something like this:

#[spacetimedb(table, name = "player_inventory")]
pub struct PlayerInventory {
  #[foreign_key(Item), cascade_delete=false]
  pub items: Vec<u64>
}

#[spacetimedb(table, name = "item")]
pub struct Item {
  #[primary_key]
  #[auto_inc]
  pub id: u64,
  pub name: String,
  // ...
}
  1. jdetter added the feature-request label Apr 14, 2025
  2. Is there any update on the potential of this getting added?

Boost this Request

Reopen this request?

The request will return to its prior live status.

Mark as Duplicate

Pick the request that Feature request: Foreign key constraints is a duplicate of. Energy rolls up into that request and GitHub closes the linked issue as a duplicate with a timeline reference to the canonical.

Unmark Duplicate

Restore as an independent request. The linked GitHub issue will be reopened and its duplicate-of relationship cleared. The original timeline entries stay as history; the note below is posted as a follow-up comment.