Sunday, February 25, 2024

Re: Data model design questio: graph in database

Question about this, why focus on using a rdbms for this functionality vs using an actual graph db like neo4j with neomodel?

On Sunday, February 25, 2024 at 7:59:31 PM UTC-5 Mike Dewhirst wrote:
Re-reading your question perhaps I was too brief in my earlier response. Let me assume all your actual edges and vertices are objects of child classes. I also assume  your base Edge and Vertex classes are abstract for inheritance purposes only.

It doesn't matter whether instances of edges and vertices are in lists. You can call obj.which_class() on any object which has (inherited) that method to determine whether an edge (or vertex) is type_1 or type_2 (using our previous terminology)

You cannot query the database using the return value from which_class() because that operates on instances not at the database level. Therefore you have to retrieve edges and vertices from the database with a query which fetches related rows and then filter the results afterwards. Perhaps like this ...

vertex_src = result_of_vertex_finder(...)
vertex_det = result_of_vertex_finder(...)
if vertex_src and vertex_dst:
    edges_1 = Edge_type_1.objects.filter(src=vertex_src, dst=vertex_dst) or []
    edges_2 = Edge_type_2.objects.filter(src=vertex_src, dst=vertex_dst) or []
    all_edges = [edges_1].extend[edges_2]

if all_edges:
    edges_type_1 = [edge for edge in all_edges if edge.which_class() == "type 1"]
    edges_type_2 = [edge for edge in all_edges if edge.which_class() == "type 2"]

On the other hand, if you want to keep all edges in the same database table it might be better/simpler to add a field 'type' to the table as you were originally thinking. 



On Sunday, February 25, 2024 at 9:47:12 PM UTC+11 Sébastien Hinderer wrote:
Dear Mike,

I will definitely play aroudn with your nice suggestion, but I would like
to share a concern I have. At this stage I am actually unsure how this
will work.

Indeed, suppose a vertex is given as input and one tries to query the
database about all the edges that originate from this vertex. The answer
will be under the form of a list of edges, but those will be of class
Edge and I assume that their type method will return the Edge class
and not the class associated to the actual child.

Likewise, once given the list of edges, one has access to the list of
vertices that are neiighbourgs of the original one, but under the form
of a list of objects of the base class, Vertex, with still no way to find
the type of the child object that has given rise to that Vertex one.

Am I perhaps missing something here?

Many thanks in advance and apologies for th likely naïve question,


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

No comments:

Post a Comment