Finlay Weber
Finlay Weber

Reputation: 4163

How to model foreign key like relationship in firestore

Lets imagine I have this data model:

I have a student with name and age, and a student can be in a class and also in a sport team.

In a relational database I would store the students into a student column. And in the class and sport tables I would reference each students via a foreign key.

This has the advantage that when a student celebrates their birth date, I only need to change the age in one place, which is the student table.

With firestore which I understand to be a nosql, the things I am reading is pointing to a modeling where I have a class document, within which all student will be embedded. Same also for the team document.

The only problem I have with this kind of modeling is if I want to update the age of a student, I would have to update in all the places, the student structure is embedded in.

Is there a better way to achieve what I can have in relational database? Where I have data defined in one place and can be reference in other places, hence giving the benefit of needing to change that data in only one place?

Upvotes: 0

Views: 551

Answers (1)

Tarik Huber
Tarik Huber

Reputation: 7398

Unfortunately no. The nosql databases are by design made in a way that you need to store the data in multiple places and sync it when required.

What I can recomment is to store as less data as possible to other places. And also try to store more static data like name or birth date that doesn't change so often (but still create a sync for them in any case).

If you don't like to just save the id of a student you can save the reference to him but in the end it's just the path to him. You can read more about it here.

Upvotes: 1

Related Questions