Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Xiao-fei_Song
Advisor
Advisor
In this blog post, we'll demonstrate how to import external CDS files into the current CDS model in your CAP project using the CDS Graphical Modeler.

For detail setup of dev space of SAP Business Application Studio in order to use the CDS Graphical Modeler, as well as creating a sample CAP project in the application studio, please refer to https://blogs.sap.com/2021/04/23/an-introduction-to-cds-graphical-modeler-for-sap-business-applicati.... In this blog post, we'll assume your dev space and CAP project are already setup successfully.

Importing CDS files


Suppose in your sample CAP project, you have below data mode in db/data-model.cds:
namespace my.bookshop;

entity Books {
key ID : Integer;
title : String;
stock : Integer;
}

And you have another db/schema.cds file:
namespace abc.xyz;

entity Authors {
key ID : Integer;
name : String;
}

 

Now let's open db/data-model.cds using the CDS Graphical Modeler:



Now we want to create an association property "author" for the "Books" entity that points to the "Authors" entity defined in the "abc.xyz" namespace in schema.cds. In order to use the "Authors" entity, we need to import db/schema.cds first. Click the "Import" toolbar button and select schema.cds in the file selection dialog:


Click "Select CDS File" button to dismiss the dialog, and you will see the import dialog:


Accept the defaults and click "OK" to finish the import. Now you are able to use the entity defined in the schema.cds file. And actually you can see the definition of the Authors entity in the CDS Graphical Modeler by navigating to the imported namespace "abc.xyz":



Click the home button and switch back to the main "my.bookshop" namespace, let's create an association for "Books" entity:


And in the "New Relationship" dialog, you should be able to select "abc.xyz.Authors" as the target type for this relationship:


Click "Create" to dismiss the dialog, and you will be able to see the new property "author" has been created for Books entity:


You can verify the content in db/data-model.cds file using the code editor:
namespace my.bookshop;

using abc.xyz from './schema';

entity Books
{
key ID : Integer;
title : String;
stock : Integer;
author : Association to one xyz.Authors;
}

We can do the same for schema.cds and create an association called "books" for the Authors entity:



namespace abc.xyz;

using my.bookshop from './data-model';

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

Conclusion


In this blog post, we demonstrate how to import external CDS file into the current CDS model in order to use the entities defined in those files, we also demonstrate how to create relationship between 2 entities in different CDS files.