Back

[SOLVED] Database Relationships

  • 0
  • Databases
  • Cloud
dch09
7 Sep, 2023, 19:14

Coming from Firebase, I have a hard time figuring out the database model for my App. Collections I already have:

  • store (eg. grocery store)
  • profiles (public user profile)
  • addresses (where store is located)
  • reviews (store reviews)

Relations are simple:

  • Store has one Address. Ideally, address is nested in Store (currently it's working on version 1.4 using one to one relationship)
  • Store can have many reviews. Each review must have an author (Profile). Each profile can have many reviews for different stores. Each reviews contains information about store its referring to.

How would you implement that kind of relationships? Using new relationship attribute in 1.4, I have issues with receiving nested documents when I don't want them (eg. displaying list of stores always gives me an array of reviews, with nested profile, per each store in list)

Should I just keep an id of a document and fetch it manually? When to choose relationship and when do it manually via id? Thanks in advance.

TL;DR
The user is asking for help with database relationships in their app. They already have collections for stores, profiles, addresses, and reviews. They want to know how to implement the relationships between these collections. They want the store to have one address and to be able to have multiple reviews, each associated with an author profile. The user is currently using a one-to-one relationship to nest the address within the store, but they don't want the nested reviews and profiles to be included when displaying a list of stores. They're wondering if they should manually fetch the document using its ID instead of using relationships. The user is looking for guidance on when
D5
7 Sep, 2023, 20:01

Each review has a store ID associated

D5
7 Sep, 2023, 20:01

Don't use relationships for that

D5
7 Sep, 2023, 20:01

Instead, just a document field (string) with the store ID

dch09
7 Sep, 2023, 20:07

So what is the ideal use case for relationships? Store & Address, right?

D5
7 Sep, 2023, 20:08

Isn't that like a label?

dch09
7 Sep, 2023, 20:08

Label? Could you elaborate?

D5
7 Sep, 2023, 20:09

A store doesn't have an address?

D5
7 Sep, 2023, 20:10

If so, then in the store document, just add an address attribute

D5
7 Sep, 2023, 20:17

So in my opinion this is how your database should be organised:

A main database (for example called main)

  • Store collection: Each document has these attributes: Name (string), Address (string)
  • Reviews collection: Each document has those attributes: Creator ID (reviewer) (string), Store ID (the ID that corresponds to the first collection document (the corresponding store document), Review ranking for example (1/10) (int) and the review text (string)
  • Profiles: User name (string) and use the document ID as the user ID in Account/Auth API. You can add other fields for things like profile picture, bio, etc
D5
7 Sep, 2023, 20:18

@dch09 Is that helpful?

dch09
7 Sep, 2023, 20:19

yes! that's helpful thank you

dch09
7 Sep, 2023, 20:19

i referred to Address as a separate collection, because it's more complex than simple string.

dch09
7 Sep, 2023, 20:19

that being said, it still could be stored as an attribute inside Store, but using new reference option right?

D5
7 Sep, 2023, 20:20

What do you mean?

dch09
7 Sep, 2023, 20:20

well it's a structure, containing multiple fields

D5
7 Sep, 2023, 20:21

I think it should be fine unless you need to do complex queries, etc based on such data.

dch09
7 Sep, 2023, 20:21

noted. thanks again! 🀝

D5
7 Sep, 2023, 20:22

Also, forgot to mention, if you're going to make something like only getting addresses, then it's better a separate collection for that

D5
7 Sep, 2023, 20:23

Can I mark this as solved, or do you need something else related to this?

dch09
7 Sep, 2023, 20:25

sure, I think I know everything I need!

D5
7 Sep, 2023, 20:36

[SOLVED] Database Relationships

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more