Text pimary key unique

I have a table that has a text primary key. If the user click in the button to add a new record goes to the detail screen and if the user enters for this text primary key field a value that alerady exist instead of showing an error or create a new record it updates the existing record that has that text primary key. Do you know how to properly handle this issues? Thanks!

mvp_badge
MVP
Solution

Hello Jake,

So you want to show an error message if the key already exists, instead of updating the existing record for that key, right?

If so, you should indeed execute an additional query to check if the key already exists in the Database. If it does, you show a message. If it doesn't, you create a new record for that key.


You can use the Empty property of the resulting Aggregate.


Kind regards,

Rui Barradas

You should use the Create action on the entity instead of CreateOrUpdate and you'll get an exception. 

But if you want another behavior please describe about what you would like to expect.

mvp_badge
MVP

Hello there Jake,

Hope you're doing well.


So, as far as I understood, the user clicks in a button to go to a Detail Screen. In that screen, the user will enter a text, which corresponds to a primary key for an entity.


As far as I can tell, you just need to call CreateOrUpdate<Entity> action and pass the primary key.

If this key doesn't exist in the database, a new record will be created. But if it does, it updates the existing record for that key.


Kind regards,

Rui Barradas

Thanks, but the issue is that how to properly handle this scenarios, if the key already exists show a message instead of updates the existing record for that key. Is necessary to do a additional query or there is a better approach?


mvp_badge
MVP
Solution

Hello Jake,

So you want to show an error message if the key already exists, instead of updating the existing record for that key, right?

If so, you should indeed execute an additional query to check if the key already exists in the Database. If it does, you show a message. If it doesn't, you create a new record for that key.


You can use the Empty property of the resulting Aggregate.


Kind regards,

Rui Barradas

Champion

Hi Jake,

I  think instead of handling an expetion,  you should programmatically check the duplicate before creating new data.

Kind regards,

Hi jake,

I agree with Tom Zhao. Exceptions aren't made to handle this kind of problem. 

In order to improve your user experience, maybe you could set a Client Action to the onblur event (the onblur event occurs when an object loses focus). So, when the user sets the value, you can check if there is a duplicate and then set the widget.Valid to false and the widget.ValidationMessage to a informative text. The user will be informed about the problem even before submit the form.

Note that you will need to check the widget.valid attribute in the submit action again.

Hope it helps.

Best regards.

Champion

You always should consider to have a Validation Action before create or update records. 

And in your case, you should check if the name already exists in your entity before create or update the record.

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