24
Views
4
Comments
Multiple unique key
Application Type
Reactive

I have an entity with 3 attributes,

1) Type, references another entity. I will call it Entity A, and it gets EntityA.Id

2) Subject, references another entity. I will call it Entity B, and it gets EntityB.Id

3) Body, text.

Now I have a form with drop down for 1 and 2, the text is free text that can be entered by the user. 
How can I ensure that for the combination of 1 and 2 will be unique. I do not want new entries to be added if the combination of 1 and 2 already exist.

Hi Jia,

I would advise you to add the unique key constraint for the both attribute, in this case database will not allow two records of same combination.

Follow the below forum post for reference.

https://www.outsystems.com/forums/discussion/69401/how-to-ensure-unique-values/#:~:text=If%20you%20referring%20to%20values,that%20you%20want%20as%20unique.

Sachin

Hello @Jia Le Chang,

As explained by @Sachin Mahawar to create indexes, please refer this link to get an idea how to create indexes on the entities in OutSystems.

https://success.outsystems.com/documentation/11/developing_an_application/use_data/data_modeling/create_an_entity_index/

Thanks,

Sachin

In addition,

adding the index will ensure the combination will be unique, what you asked for, but you should do a bit more than that if you want to have a happy user : I would advice to make sure it is never up to the database to throw an error, but to validate up front.

In the crud wrapper, right before creating or updating a record, run an aggregate finding another record with same EntityA and EntityB attributes but with a different identifier.  If found, return error or warning to the user instead of trying to create or update.

Or you could go the extra mile, and try to avoid the user choosing an existing combination in the first place, but that can get a bit more complicated, because on every dropdown change, you'd have to act.  For example, by limiting the choices in the other dropdown.  

For a table with a small number of total records, that might be an option, because you could retrieve all existing combinations, but if you would have to make a server run on every dropdown change, it would probably degrade instead of improve user experience.

Dorine

Thank you all for the input. I got it working. 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.