cancel
Showing results for 
Search instead for 
Did you mean: 

Rename a column generated by SAP CAP - CDS

iperez-sofos
Participant
0 Kudos
2,312

Greetings.

I am working on a project with SAP CAP and SAP HANA database. I am currently working under the SAP HANA Express Edition environment (on my local PC), but eventually the development will be migrated to a cloud environment.

I want to know if it is possible to change the CDS generated name for a column of an entity in my data model.

It's possible?

The situation is the following:

In a data model (CDS file) I have an entity that represents the Regions, as follows:

entity Regions: SAPCOM.CodeList {
    key code: String (5); //> ISO 3166-1 alpha-2 codes (or alpha-3)
        country: SAP_Country;
        superRegion: Association to Regions;
        subRegions: Composition of many Regions on subRegions.superRegion = $ self;
}

In another model (other CDS file), I have an entity called SeasonalRegions that represents a region for which a well-known set of annual seasons is defined. SeasonalRegions is a subcategory of Regions. There is also an entity that represents the annual seasons. The model is as follows:

entity SeasonalRegions {
    key region: Association to Regions;
        seasons: Composition of many Seasons on seasons.region = $ self;
}

entity Seasons: {
    key name: SMECOM.NameID;
    key region: Association to SeasonalRegions;
        descr: SMECOM.Description;
        start: SMECOM.DayMonth;
        end: SMECOM.DayMonth;
}

The problem I have is that, by CDS naming rules, the name generated for the region column of the Seasons entity is region_region_code. I'd like to change it (maybe just region_code, or something similar).

Is there a way to achieve what I want? (either with a technical SAP CAP build such as annotations, or from a modeling point of view).

Thanked in advance for any help and / or suggestion.

P.S:

Some details (such as the exact names of the entities) of the code segments in my post are not exactly as I have them in my program. I changed them to facilitate your understanding here.

Accepted Solutions (1)

Accepted Solutions (1)

hjb
Product and Topic Expert
Product and Topic Expert

Hi,

conceptually a managed association can be perceived as a structured type with a well defined description of the relationship to a target definition.

Any structured type is flattened by (recursively) prefixing the i-th level element name with the (i-1)th level element name with 0 < i <=n (*).

By using primary key managed associations as foreign keys in other associations, you are creating a two-level structured type resulting in the column name 'region_region_code'.

If you don't like this column name, you are absolutely free and entitled to use unmanaged associations instead giving you full control over all parts of the association.

Since foreign keys are just members of a structured type you can refer to them in ON conditions. This allows you to write something like this:

entity Seasons { 
 ....;
 key region_code: Integer;
 region: Association to SeasonalRegions on
    region_code = region.region.code;
 ...;
};

The similarity of region.region.code and region_region_code is not just accidental.

But you CANNOT access non foreign key elements via an association in an ON condition as this would embody a nested join inside the relation you are just about to describe. Those nested relationships must be resolved via additional views.

Regards, Hans-Joachim

(*) foreign keys are somewhat special as we allow sub structure references and aliases but that can be neglected here.

AndrewBarnard
Contributor

hans-joachim.both many thanks for your well written answer. Very helpful.

iperez-sofos
Participant
0 Kudos

Thanks for your reply, Hans-Joachim. It has been helpful and enlightening.

Answers (0)