Skip to content

Allow renaming the referencing attribute in a foreign key #206

@dimitri-yatsenko

Description

@dimitri-yatsenko

From the start, DataJoint allowed foreign key constraints only between identically named attributes. This convention simplified the definitions and the relational algebra. Most data definition problems can be adequately addressed under this limitation.

However, some problems would become easier after renaming the referencing attribute. Therefore, I suggest the following definition syntax for defining foreign key constraints

[attribute [=default-value]]  ->  Table

If the referenced table schema.Table has exactly one primary key attribute that is not already in the current table, then the referencing attribute will be named attribute.

For example, imagine we have a table Vertex listing the vertices of graphs. Then the table Edge may reference Vertex twice as from and to.

class Vertex(dj.Manual):
    definition = """  # vertex in a graph
     -> Graph
     vertex_id   : int  
     ---
     value : float  
     """

class Edge(dj.Manual):
    definition = """  # directed edge connecting two vertices
     -> Graph
     from -> Vertex 
     to -> Vertex
     ---
     weight : float   # edge weight
     """

The relational algebra remains unchanged. For example, to compute the mean of the two connected vertex values for each edge, the expression will be

src = Vertex().proj('node_id -> from', 'value -> v1')
dest = Vertex().proj('node_id -> to','value -> v2')
result = (src*Edge()*dst).proj('(v1+v2)/2 -> edge_value')

Proper renaming of attributes before joins and restrictions will become necessary. Therefore, this approach will be used rarely when it results in significant improvements compared to the original DataJoint approach such as the example with graphs above.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions