ThingsDB 中的不匹配错误:具有关系的类型必须使用存储的事物上的属性创建

huangapple go评论51阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月23日 20:44:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定