clockworklabs / clockworklabs/SpacetimeDB

Feature request: Foreign key constraints

Open
#2,608 2 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request
Dominant language
Rust
Stars
25.2k
Forks
1.1k
Avg merge
2d 7h
Merged PRs (30d)
46

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Rust table definitions and the proposed #[foreign_key] and cascade_delete attributes in this issue, then locate the repository's existing #[spacetimedb] table and schema-attribute handling. Done means agreeing on foreign-key behavior for scalar and Vec references, including deletion semantics, and implementing the feature with corresponding tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.