r/Database • • 1d ago

How do I reconcile this database schema?

There are 3 projects: 1. ItemGallery. 2. Sender. 3. Receiver. The sender fills a form and creates an item. The item gallery shows it, and the receiver sees it.

The item's data model has a foreign key referencing a sender id. How should I create test items in the gallery project, without declaring the sender's project as a dependency?

0 Upvotes

9 comments sorted by

2

u/NW1969 1d ago

I'm not entirely sure what you are asking, based on the terminology you are using, but if an entity (A) has a FK to another entity (B) , and the FK column is defined as NOT NULL then you can't enter data into A if the corresponding record(s) in B don't exist - that's the whole point of FKs

1

u/Philluminati 1d ago

3 projects with 3 separate code bases? But so toghtly coupled they use the same db? Maybe they could be 3 sub projects of a larger project and share a library that does db handling?

Maybe a good test suite is enough to ensure the theee projects don’t break each other?

Or perhaps they could communicate in a more formal way?

1

u/[deleted] 1d ago

[removed] — view removed comment

1

u/No-Security-7518 1d ago

Thank you so much for the perfectly thorough answer! I ended up merging the gallery with the sender's project, since a sender invariably needs to both contribute to the gallery as well as view it. The receiver project will require the sender project as a dependency. I pulled more common code between the 3 projects as a library they all depended on. Again, thank you for the insights, because I run into this scenario a lot. And one more question please: in a data model, when do I decide that a DAO should get an id of another DAO it requires vs compose it with the entire dao? I understand that in the latter design, I "guarantee" I'll have an object with its full behavior. Also, sometimes, I need to use getters to the nested DAO so I expose them and delegate to it. e.g. //Offer String getItemName() { return item.getName(); } and this is not possible if I use just an id. What are your thoughts on this?

1

u/[deleted] 7h ago

[removed] — view removed comment

1

u/No-Security-7518 4h ago

Aha, so can I make this into a rule? Because I need one, it's not just this example, I run into this in almost every project. So, how about this for a rule:
- Do I need the 2 objects together? (I could literally count screen), if the answer is yes, to most, I nest, otherwise, reference?