Skip to content

Feature request: Foreign key constraints #2608

@jdetter

Description

@jdetter

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,
  // ...
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions