dvdclrk

dvdclrk

Good afternoon all,

So I’m building a “small” web app to track books read, shows and movies watched, comics enjoyed, etc. But I’ve hit a bit of “devblock” in that I’m not sure how to I should properly map out the tables.

Initially, I wanted to do a single “Items” table that would hold a primary binary_id key and a type and a few other common data points which would lead you to some distinct tables for each type. But then as I was reading the Ecto docs I stumbled on the polymorphism section about how that’s discouraged for a number of good reasons.

Then I figured I’d do Single Table Inheritance and have an “Items” table with all the columns needed but that would lead to a lot of nulls bouncing around which I wasn’t a fan of. I could use schemas to hide them away so maybe this is the right way but I wanted to explore other options as well.

My current favorite is the separate tables for each “Item” type and then an “Items” abstract table. There are only a very few times in the initial design of my application where “Items” is used so I don’t believe the DB would be hampered by having to join the distinct tables too often.

But perhaps utilizing embedded schemata is the way to go? I’ve seen the polymorphic embed package.

The relevant post where I got these ideas is linked here. It’s a fairly exhaustive list of pros and cons but I was wondering if anyone else has gone through this process and had any wisdom on mapping inheritance in databases. I may just have to settle on one option and test it to see if it fits my needs but was hoping someone had something they could add.

Thanks for any insight in advance.

Showing Posts 1 to 3

lud

lud

My take on this would be the following:

If it is a commercial product where you want to enforce data consistency, then you will need different tables and a bunch of schemas, because you do not want nulls everywhere.

If it is a self hosted tool, or a learning app, then just put everything in a single table, and move on. Nobody cares if some book misses some data because you missclicked and created a movie before realizing it was a book you wanted to store. You actually don’t even need a database for that, a file would be enough.

al2o3cr

al2o3cr

Some other things to think about:

  • do you need relationships between items that’s enforced by the database?
  • do you need to query for items that might be of mixed types?
  • do you need user-defined attributes? (“shelf”, “box #” etc)

Especially in that latter case, you might consider a less “schema-y” approach like EAV - store an “item” and then have a single “attributes” table.

In that case, the difference between a “movie” and a “book” is the type and the initial set of possible attributes

mathieuprog

mathieuprog

My quick take:
If your polymorphic data contains relations (foreign keys) that you want to join on, then use tables. Otherwise, use JSONB with polymorphic_embed library.
EAV is generally not recommended if you can use one of the two above.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews