Error Message - Does query need ran first?

Hi!

I am trying to display a custom error message to the user when they enter a duplicate Ingredient.

This conditional I have setup works, but I think it needs to have the query ran first; otherwise, the full automated SQL error message gets displayed (see below), and not the custom error message I wrote. However, the second time the user tries to enter the duplicate, the custom error message is displayed.
Screenshot 2024-01-09 at 9.20.56 PM

Do I have to run the query first, or is there a way around that?

Screenshot 2024-01-09 at 9.16.54 PM

I'm guessing it might be a race condition that the error is displaying before the value of .error has been set so the first time it shows the error statement and subsequent failures shows your custom warning. I'd maybe consider having a generic failure message though, otherwise the only warning you'd see if for duplication not other issues and you'd probably end up coding every possible error message into that query.

Something you may also want to consider is to check the entered name for duplicates against the already loaded list of ingredients (it appears they're listed in a table in your screenshot) before trying the insert and save yourself a round trip to the database?

1 Like

Great idea @dcartlidge. I'd like to further expand it.

When a user presses Create button, some kind of validation query should be run instead of insert query. This validation query would try to select potential duplicate records. If potential duplicates exist then show custom error message. Otherwise perform the insert query.

1 Like

Exactly, that's how I'd probably handle this situation - if the list of all ingredients has been loaded then it should be a very quick lookup to check if the name has already been used and can even be done inline as they type

2 Likes

Ok perfect! That worked.

I set up a separate validation query to run first, and if that's successful, then the insert query runs. If not successful, then the custom notification runs.

I used a SQL query to count

SELECT COUNT(*) as ingredient_count FROM ingredients WHERE ingredient_name = {{ EditIngredient.value }}

Thanks!

Hey @George_Kriwiel - I suggest that you mark one of the posts as a Solution so that it would be easy for other people to find it