Ecto join-table inheritance

301 views
Skip to first unread message

Raphael Vidal

unread,
Nov 16, 2015, 7:19:31 PM11/16/15
to elixir-ecto
Hey guys,

I am migrating a legacy database from a PHP application, wich uses the Doctrine2 ORM, to Elixir using Phoenix and Ecto.

But I am facing a problem. The Doctrine2 ORM supports Join-Table inheritance, wich uses data from multiple tables to compose a model and btw, it was used a lot on the old application. Eg.: The database has three tables, "people", "students" and "teachers". The only table wich has a PRIMARY_KEY is "people", the other two actually have FOREIGN_KEYs in their "id" field pointing to the "id" field on the "people" table, that also has a "dtype" field of type "string" that says if some specific line is a Student or a Teacher. When I ask Doctrine2 to load a Student, it then fetches data from "student" and from "people" tables using a INNER JOIN and builds a single model with the returned data, so that when I query, load, update and save the model I don't have to worry about how this will be done.

Chatting on the #ecto channel on the Elixir Slack, I was given the idea of using a regular association to the "people" table, virtual fields for the inherited properties and callbacks to load and save the data to the right place, but writing the queries would still be a pain and the loading times would increase considerably, specially on large collections.

It would be nice to have this builtin into the ORM, as it is a great feature and maybe a key selling point for developers trying to convince their bosses to migrate their codebase :D

I love the Elixir lang and I am loving the community around it and how it is growing, so, if I can help with anything in the development, I would also love to do so ^^

If it is decided to implement this feature, I was thinking of something adapter-dependent, where each adapter could work this out in it's own way (Pg could use it's native table inheritance, while MySql and Sqlite could use Join-Table inheritance).

What do you think?

Matt Widmann

unread,
Nov 30, 2015, 5:00:32 PM11/30/15
to elixir-ecto
I'll +1 this feature.

But I think José has said in the past that concrete table per class is the recommended way to from Ecto's standpoint. I'm not sure how well an inheritance structure will play in a functional language like Elixir.

Raphael Vidal

unread,
Dec 21, 2015, 7:22:38 AM12/21/15
to elixir-ecto
Well, I can see how a feature named "Table Inheritance" would mess up people's heads in a functional language. 

But, how about we change this feature to something in the likes of "Join table schema" or "Schema composition"? We can change how the feature would be documented, or even it's implementation, so that it'll be just composition, not hurting any functional principles and still solving the problem. 

It's just a matter of making the developer's life easier, specially while migrating legacy apps.
Reply all
Reply to author
Forward
0 new messages