aiida-core icon indicating copy to clipboard operation
aiida-core copied to clipboard

👌 IMPROVE: Check nodes are from same backend in `validate_link`

Open chrisjsewell opened this issue 2 years ago • 1 comments

Since here: https://github.com/aiidateam/aiida-core/blob/bab1ad6cfc8e4ff041bce268f9270c613663cb35/aiida/storage/psql_dos/orm/nodes.py#L204

Only the node ID's are used to create the DbLink row, it would be possible currently to accidentally pass it two nodes associated with different backends, and so create an incorrect link.

We could possibly use the actual DbNodes, to instantiate the DbLink, and have sqlalchemy check that they are associated with the same Session, but adding the check in validate_link should catch any issue earlier on, and make it backend agnostic.

chrisjsewell avatar Mar 22 '22 15:03 chrisjsewell

FYI, this is how you would use source.bare_model/self.bare_model, instead of .id, and what would happen if the backends were different:

engine = sa.create_engine('sqlite:///:memory:', future=True, echo=False)
Base.metadata.create_all(engine)

session1 = Session(bind=engine, future=True)
session2 = Session(bind=engine, future=True)

node1 = DbNode(node_type='data.dict.dict', label='test1')
session1.add(node1)

node2 = DbNode(node_type='data.dict.dict', label='test2')
session2.add(node2)

link = DbLink(input=node1, output=node2, label='test', type='test')

session1.add(link)
InvalidRequestError: Object '<DbNode at 0x7fc6f3754700>' is already attached to session '9' (this is '8')

Perhaps could add this also 🤷

chrisjsewell avatar Mar 22 '22 15:03 chrisjsewell