I'm playing with relations between types in ThingsDB. When creating a new instance of a type, how am I supposed to create the relation?
This is my simplified collection set-up:
new_type('Person');
new_type('Workspace');
set_type('Person', {
name: 'str',
workspaces: '{Workspace}',
});
set_type('Workspace', {
name: 'str',
owner: 'Person?'
});
mod_type('Person', 'rel', 'workspaces', 'owner');
.alice = Person{
name: 'Alice'
};
When creating a workspace with owner Alice, I get the following error:
w = Workspace{
name: 'Foo',
owner: .alice
};
// mismatch in type `Workspace` on property `owner`; relations must be created using a property on a stored thing (a thing with an Id)
Relations in ThingsDB work in both directions. Instead of assigning the owner, you should add the workspace to Alice. That is what the error message is referring to: "using a property on a stored thing". This is the thing which is assigned to the collection and therefore has an Id.
w = Workspace{name: 'Foo'};
.alice.workspaces.add(w);
// The workspace owner (w.owner) is now automatically set because of the relation