cancel
Showing results for 
Search instead for 
Did you mean: 

Associations for synonyms in CAP

mani_ac
Participant
0 Kudos

I have a synonymn and defined in data model with @cds.persistence.exists and I have also defined the association in the data model (only at CAP not in source table) . But when I run the service metadata I don't see the Navigation property.


@cds.persistence.exists
entity Books {
ID : Integer;
author : Association to Authors on author.ID=ID;
}
@cds.persistence.exists
entity Authors {
ID : Integer;
books : Association to Books on Books.ID= ID;
}

I also tried

extend Books with {
author : Association to Authors on author.ID = ID
}


I am not sure if it is possible to add an association to synonyms . Can you please suggest me what I am doing wrong here?

pepl
Active Participant
0 Kudos

Hi Mani! What is your service definition?

You need to have all entities defined in your service to be used as associations.

You may also use @cds.autoexpose: https://cap.cloud.sap/docs/cds/cdl?q=autoexpose#auto-expose

I hope it helps 😃

Accepted Solutions (0)

Answers (1)

Answers (1)

patricebender
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

you have to wrap your entities into a service like so:

service MyService {
  @cds.persistence.exists
  entity Books {
    key ID     : Integer;
    author : Association to Authors
               on author.ID = ID;
  }

  @cds.persistence.exists
  entity Authors {
    key ID    : Integer;
    books : Association to Books
              on books.ID = ID;
  }
}

Alternatively, you could also create projections on your entities in a service.

Then you will see the Navigation Property in the metadata:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
  <edmx:DataServices>
    <Schema Namespace="MyService" xmlns="http://docs.oasis-open.org/odata/ns/edm">
      <EntityContainer Name="EntityContainer">
        <EntitySet Name="Books" EntityType="MyService.Books">
          <NavigationPropertyBinding Path="author" Target="Authors"/>
        </EntitySet>
        <EntitySet Name="Authors" EntityType="MyService.Authors">
          <NavigationPropertyBinding Path="books" Target="Books"/>
        </EntitySet>
      </EntityContainer>
      <EntityType Name="Books">
        <Key>
          <PropertyRef Name="ID"/>
        </Key>
        <Property Name="ID" Type="Edm.Int32" Nullable="false"/>
        <NavigationProperty Name="author" Type="MyService.Authors">
          <ReferentialConstraint Property="ID" ReferencedProperty="ID"/>
        </NavigationProperty>
      </EntityType>
      <EntityType Name="Authors">
        <Key>
          <PropertyRef Name="ID"/>
        </Key>
        <Property Name="ID" Type="Edm.Int32" Nullable="false"/>
        <NavigationProperty Name="books" Type="MyService.Books">
          <ReferentialConstraint Property="ID" ReferencedProperty="ID"/>
        </NavigationProperty>
      </EntityType>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

Did this help?:)

Best Regards,

Patrice