cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Yahya
Level 10

What is Key in CDS?

Serious, what is Key in CDS? please share an example not an article

3 REPLIES 3
PowerApps Staff CWesener
PowerApps Staff

Re: What is Key in CDS?

 

Hi @Yahya - by default CDS entities have a GUID as their only unique field - but in another system you may use something like a Customer ID, or Order Number to uniquely identify a particular record. Defining a key allows you to make sure your field is also unique, and CDS won't allow duplicates of data to be entered within it. This can be especially helpful if you're integrating your data with an external system that uses that ID or number to identify a record (and not the CDS GUID). It also improves the search and filtering on the particular field, as we have indexes in place.

 

Keys can be based on a single field (Order ID) or a combination of fields (example : Financial year and Order ID). If you set a key on a field and try to enter duplicate data, the record will fail to save.

 

Does this help?

Thanks,
Clay.
Yahya
Level 10

Re: What is Key in CDS?

Hi @CWesener, Thanks for the response. This reminds me with the property "Unique" in Access Web Apps database.

 

2 things I would like to mention: -

 

1. Creating a key via the PowerApps studio (online) will not prevent duplicating a record. This occurred in my tenant at least and I am willing to organize an online session so your team checks by themselves.

 

I trued the Solution explorer, checked my index which I created from PowerApps studio to find that there is one more step to be done on the key, to activate/resume. it failed anyhow because of existing duplicates I think.

 

I deleted, recreated, activated and it has worked perfectly then.

 

There is therefore somethng missing in creating a key from the PowerApps studio that need to be fixed.

 

2. Not all fields can be set as a Key for instance a Look up field. Now think of this scenario, I have 2 custom entities on the top of my database model, one is COUNTRY and the other is CITY. While I have entered all countries we work with, like 80 or so, cities are to be added by the users. I could create a key in CITY that is the City Name but for this is not correct in real life, as for instance, I have 2 cities called Tripoli, one is in Libya and the other is in Lebanon. So the right thing is to create a key from the concatenation of City Name and Country in the CITY entity, something not available because also calculated fields are not possible available to key feature.

 

The same goes for a CUSTOMER and PROJECT REFERENCE, so none of my customers would have 2 different projects under the same PROJECT REFERENCE.

 

what would be your thoughts handling this?

 

 

PowerApps Staff CWesener
PowerApps Staff

Re: What is Key in CDS?

 

Hi @Yahya,

 

Regarding the first issue - can take a few minutes for the key to be created, so if you immediately go and enter data, it wont be unique, and then the key will fail. We are looking at improving this experience in the portal.

 

For the second item, this would be a new feature for us to support more data types in keys - can you please raise an idea on the ideas forum?

 

Thanks,

Clay.

Thanks,
Clay.