cap-js / change-tracking

CDS plugin providing out-of-the box support for automatic capturing, storing, and viewing of the change records of modeled entities.
https://cap.cloud.sap/docs
Apache License 2.0
22 stars 8 forks source link

Error with table with a key in navigation property #134

Open VanessaSap opened 2 weeks ago

VanessaSap commented 2 weeks ago

Hello,

When I try to implement Change Tracking on a table where the key is a navigation property, I get the following error:

"Error: CDS compilation failed (@sap/cds-compiler v5.4.0) srv/admin-service.cds:408:45: Error: Unexpected usage of structured type “to_Rule” (in entity:“RulesDerivation”/column:“changes”/on)"

entity RuleDerivation : managed { key to_Rule : Association to design.Rule ... }

Could you help me understand if there’s a way to work around this problem?

VanessaSap commented 2 weeks ago

Let me know if you need more details

jbm1991 commented 1 week ago

Did you find any solution to this problem as I've just run into exactly the same thing? Some further details for reproduction:

//schema.cds
namespace my.bookshop;

entity Books {
  key ID      : Integer;
      title   : String;
      stock   : Integer;
      authors : Association to one Authors;
      pages   : Association to many Pages
                  on pages.book = $self;
}

entity Authors {
  key ID    : Integer;
      name  : String;
      books : Association to many Books
                on books.authors = $self;
}

entity Pages {
  key book : Association to one Books;
  key num  : Integer;
      text : String;
}
// cat-services.cds
using my.bookshop as my from '../db/schema';

service CatalogService {
    @readonly
    entity Books   as projection on my.Books;

    @readonly
    entity Authors as projection on my.Authors;

    @readonly
    entity Pages   as projection on my.Pages;
}
// change-tracking.cds
using {CatalogService} from './cat-service';

annotate CatalogService.Books {
    stock @changelog;
    title @changelog;
}

annotate CatalogService.Authors {
    name @changelog;
}

annotate CatalogService.Pages {
    num  @changelog;
    text @changelog;
}
nkaputnik commented 4 days ago

Hello, NON-UUID keys and multiple keys have been a known limitation of Change-Tracking in the past. We have a current branch in development to fix this issue for the next release. I'm not sure whether this will completely fix associations as keys in the first run. In the meanwhile, the only workaround would be to use the association as a surrogate key, and have a single "real" key. The next release should fix this.

Best, Nick

VanessaSap commented 3 days ago

Did you find any solution to this problem as I've just run into exactly the same thing? Some further details for reproduction:

//schema.cds
namespace my.bookshop;

entity Books {
  key ID      : Integer;
      title   : String;
      stock   : Integer;
      authors : Association to one Authors;
      pages   : Association to many Pages
                  on pages.book = $self;
}

entity Authors {
  key ID    : Integer;
      name  : String;
      books : Association to many Books
                on books.authors = $self;
}

entity Pages {
  key book : Association to one Books;
  key num  : Integer;
      text : String;
}
// cat-services.cds
using my.bookshop as my from '../db/schema';

service CatalogService {
    @readonly
    entity Books   as projection on my.Books;

    @readonly
    entity Authors as projection on my.Authors;

    @readonly
    entity Pages   as projection on my.Pages;
}
// change-tracking.cds
using {CatalogService} from './cat-service';

annotate CatalogService.Books {
    stock @changelog;
    title @changelog;
}

annotate CatalogService.Authors {
    name @changelog;
}

annotate CatalogService.Pages {
    num  @changelog;
    text @changelog;
}

Hello, Unfortunately, I had to make a structural change to implement this history.

nkaputnik commented 3 days ago

Please see also here: https://github.com/cap-js/change-tracking/issues/130