assoc2many-ambiguous-key
Rule Details
In general an association/composition to/of MANY
that targets an entity without ON
condition is not allowed (as it is an n:1
relationship). Here, one should always specify an ON
condition following the canonical expression pattern <assoc>.<backlink> = $self
. The backlink can be any managed to-one association on the many side pointing back to the one side.
Examples
✅ Correct example
In the following example, we define a unique association from Authors
to Books
with a well-defined ON
condition and backlink, thus satisfying the rule's conditions:
namespace sap.capire.bookshop;
entity Books {
key ID : Integer;
@mandatory title : localized String(111);
@mandatory author : Association to Authors;
}
entity Authors {
key ID : Integer;
@mandatory name : String(111);
books : Association to many Books on books.author = $self;
}
2
3
4
5
6
7
8
9
10
11
12
13
❌ Incorrect example
If we extend this example by creating a view AuthorView
with a key ID
and the element bookIDs
without an ON
condition, the rule is triggered since the key is no longer unique and bookIDs
leads to multiple entries:
namespace sap.capire.bookshop;
entity Books {
key ID : Integer;
@mandatory title : localized String(111);
@mandatory author : Association to Authors;
}
entity Authors {
key ID : Integer;
@mandatory name : String(111);
books : Association to many Books on books.author = $self;
}
view AuthorView as select from Authors {
// Ambiguous key in 'AuthorView'. Element 'bookIDs' leads to multiple
// entries so that key 'ID' is not unique.
key ID,
books.ID as bookIDs
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Version
This rule was introduced in @sap/eslint-plugin-cds 1.0.1
.