cancel
Showing results for 
Search instead for 
Did you mean: 

Why is Foreign Key not generated for Association in CDS (CAP) when running "cds compile" command ?

NitinRamesh
Employee
Employee
0 Kudos
106

I have the following CDS entities

entity Book {
    key id     : Integer;
        author : String;
        title  : String;
}

entity Summary {
    book    : Association to Book;
    summary : String;
    lang    : String;
}

I'd like to have the summary of a book in different languages.

When I run the following command

cds compile model.cds -2 sql

I get the following DDL as output

CREATE TABLE my_app_Book (
  id INTEGER NOT NULL,
  author NVARCHAR(255),
  title NVARCHAR(255),
  PRIMARY KEY(id)
);

CREATE TABLE my_app_Summary (
  book_id INTEGER,
  summary NVARCHAR(255),
  lang NVARCHAR(255)
);

 However I was also expecting a FOREIGN KEY on book_id to relate the two tables.

Is this a bug or expected behaviour ?

If this is actually the expectation, then is there any way I can configure my CDS such that the DB table schema can clearly link the two tables ?

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

The foreign key is generated in your example. The foreign key of "Book" in "Summary" is "book_id". What is the problem here?

If you want to make it more explicit and also generate a foreign key constraint to enforce referential integrity, please consult our documentation to learn more about foreign key constraints.

NitinRamesh
Employee
Employee
0 Kudos
Thanks.
NitinRamesh
Employee
Employee

Setting

{"cds": {"features": {"assert_integrity": "db"}}}

in my .cdsrc.json did the trick