Referential integrity 'sh best practice / pattern

I was hoping some of you smart folks can give me advice on the best practice for loosely coupled objects with id references.

What I would like to do is have separate structs that represent different entities.
I would like to create some kind of relationship between entities using the concept of DB foreign keys.
Enabling fast querying of related objects would also be a must-have.

Instead of tightly coupling things I would rather have it in a fast lookup table.

So an abstract idea example is
I have a player struct with an Id property.
There can be 1:N players each with a unique id
Each player can have 1:N associated objects in play at any given time.
When a player is terminated all the associated objects must also be terminated.

So in a phrase, the objects are "associated with" but not "coupled to" the player.

Any suggestions on creating that deal with this already or perhaps some design patterns you would recommend.

1 Like

You could have a queue of ids containing the ids that need to be terminated?

1 Like

let's assume I have something like this

pub struct Object {
    id: i64,
    foreign_key: i64

I want a data structure that will contain all objects that I can query to say.
"for this id (10), find me all the objects that have a foreign key of (10)"

What is the right container that will allow me the fast search that will satisfy this "query".
After that the rest gets easy :slight_smile:

ps, this might contain a large number of objects, LARGE

How about a HashMap?

Hashmap is great if you want to use the id as the key and that is the object you want to find but not so great when you want to filter on another field like a foreign key that is not part of the hash

Well you can have anoter map for your foreign key.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.