英文:
Mismatch error in ThingsDB: type with relations must be created using a property on a stored thing
问题
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)
英文:
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)
答案1
得分: 0
在ThingsDB中,关系可以双向工作。而不是分配所有者,您应该将工作区添加到Alice。这就是错误消息所指的内容:“在已存储的事物上使用属性”。这就是分配给集合并因此具有ID的“thing”。
w = Workspace{name: 'Foo'};
.alice.workspaces.add(w);
// 由于关系,现在自动设置了工作区所有者(w.owner)
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论