In TypeQL, relations are first-class citizens. In this way it differs from languages such as SQL where relations are expressed via foreign keys and join tables.
TypeQL relations are many-to-many by default, which means that we need only write:
define
person sub entity, owns name;
publication sub entity, owns name;
name sub attribute, value string;
authorship sub relation, relates author, relates authored;
allowing us to insert entities
insert
$king isa person, has name "Stephen King";
$straub isa person, has name "Peter Straub";
$talisman isa publication, has name "The Talisman";
Now there are actually two options for the relation model: either we create a separate relation for each person-publication link, or we use one N-ary relation per publication. The first approach yields
(author: $king, authored: $talisman) isa authorship;
(author: $straub, authored: $talisman) isa authorship;
while the second approach gives
(author: $king, author: $straub, authored: $talisman) isa authorship;
In this particular instance I would actually adopt the second approach. This is because it feels quite “natural”; when you look at a book, you’ll see all of its authors listed on the cover, and there are other meaningful attributes we can attach to an authorship - for instance we could add
define
publication-date sub attribute, value datetime;
authorship owns publication-date;
allowing us to insert the relation
(author: $king, author: $straub, authored: $talisman) isa authorship, has publication-date "1984-11-08";