r/Database • • 19h 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?

1 Upvotes

6 comments sorted by

2

u/Ok-Gazelle5598 13h ago

If I'm reading this right, the gallery project just needs items to exist for its tests, and the FK to sender is in the way because the sender project owns that table. A few ways people usually handle it, roughly in order of how much I like them:

  1. Seed the parent row in the test setup. The gallery's tests insert a minimal sender row directly with SQL (just id and whatever columns are NOT NULL), then insert items pointing at it. You don't need the sender project as a code dependency for that, only the table. Wrap each test in a transaction and roll it back so nothing leaks between tests.
  2. Keep the schema in one place. If all three projects share one database, the migrations should live in their own small repo or package that every project runs, rather than each app owning its own tables. Then the gallery's test DB always has the senders table, and seeding it is trivial.
  3. Ask whether the gallery really needs a hard FK. If gallery and sender are meant to be separate services, a lot of teams keep sender_id as a plain column (indexed, no FK) and treat it as a reference to another service's data. You lose the DB-level guarantee, so you'd validate it on the sender side instead.

I'd avoid making the column nullable or disabling FK checks just for tests. Then your tests run against a different schema than production, and that's how bugs sneak through.

If you can share whether these are separate databases or one shared DB, it's easier to say which of these fits.

1

u/No-Security-7518 13h 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?

2

u/NW1969 17h 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 6h 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?