cancel
Showing results for 
Search instead for 
Did you mean: 

@assert.unique not working with deep structures

tatiana_fetecua
Explorer
0 Kudos

Hi everyone,

I have a cds schema that has the composition entities defined inside the parent entity, for example:

entity Product : cuid {
  type       : StyleType;
  style      : Style;
  categories : Composition of many {
                 key ID           : UUID;
                     categoryType : String(1);
                     categoryNumber: String(1);
               }
}

And I want to define an @assert.unique for ID up__ID and Category type :

@assert.unique.style : [
  ID,
  up__ID,
  categoryType,
  categoryNumber,
]

But I get compile messages errors like this:

message: '“@assert.unique.style”: “categoryType” has not been found'.

If I define my categories entity outside and I define the @assert.unique.style like this:

entity Product : cuid {
  type       : StyleType;
  style      : Style;
  categories : Composition of many Categories on categories.parent = $self
}
@assert.unique.style : [
  parent,
  categoryType,
  categoryNumber,
]
entity Categories: cuid {
  parent: Association to Product;
  categoryType : String(1);
  categoryNumber : String(1);
}

it works, but I want to know if it is possible to define it when the composition of many is not defined outside, because this will save me a lot of work to redefine my schema, views, etc.

Thanks,

Tatiana F.

View Entire Topic
patricebender
Product and Topic Expert
Product and Topic Expert
0 Kudos

You can do that by using the `annotate` directive:

annotate Product.categories with @assert.unique.style : [
  ID,
  up_, // you have to set the unique constraint on the association
  categoryType,
];

However, I think the unique constraint in this combination is pretty useless, as "ID" and "up_" are the primary key tuple, hence there can never be more then one record stored with the same "ID" and "up_" combination. Consequently there can never be two rows where "ID", "up_" and "categoryType" are the same.

tatiana_fetecua
Explorer
0 Kudos

Hi Patrice,

First, thanks for the response, I have implemented the annotation directive and it is working now, and yes my example was not complete, it was more to illustrate the entity definition.

Tatiana F.

patricebender
Product and Topic Expert
Product and Topic Expert
0 Kudos

glad I could help! 🙂