# Database prevent adding existing value

**URL:** <https://forum.bubble.io/t/database-prevent-adding-existing-value/105599>\
**Category:** Database\
**Created:** [August 10, 2020, 10:48am UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599 "2020-08-10T10:48:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![doklyne](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@doklyne](https://forum.bubble.io/u/doklyne)\
**Post date:** [August 10, 2020, 10:48am UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/1 "2020-08-10T10:48:57Z")

</div>

Hi,

I don’t understand how it works… the comparison text I try to use is grayed out.

1. I add a city through this form :  
 ![add-city-field](https://us1.discourse-cdn.com/bubble/original/3X/e/6/e6d857856081558ede3fe23fe7e589cc49b19a8c.png)

2. Here is the function to add the data to the DB :  

3. Here is the DB copy :  

What’s wrong ?  
I’d like to prevent duplicates.

1. So I added a condition :  
 ![cant-get-element](https://us1.discourse-cdn.com/bubble/original/3X/7/3/73845c9e87f889779093f55bd4b0c4d28196af3b.png) ![cities](https://us1.discourse-cdn.com/bubble/original/3X/b/c/bc1c1e13f10a8e79641972f3e581167fd8ebacc9.png)  
But I can’t compare the Cities’name to the search ; I don’t understand how it works.

I just want to check if the city doesn’t already exists.

Thanks for your help !

Thomas

---

<div class="post-metadata">

**Author:** ![SerPounce](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/serpounce/32/62068_2.png) [@SerPounce](https://forum.bubble.io/u/SerPounce)\
**Post date:** [August 10, 2020, 10:57am UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/2 "2020-08-10T10:57:44Z")

</div>

Why don’t you check if the city exists prior to DB update? Then you won’t have to deal with this problem and will be better for UX.

---

<div class="post-metadata">

**Author:** ![doklyne](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@doklyne](https://forum.bubble.io/u/doklyne)\
**Post date:** [August 10, 2020, 11:10am UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/3 "2020-08-10T11:10:56Z")

</div>

Thanks ! How do you that ? I haven’t been able to understand and find online how I can check if the value exists in the DB outside of the process I just described before. If you have the link to a tutorial it would help a lot

---

<div class="post-metadata">

**Author:** ![SerPounce](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/serpounce/32/62068_2.png) [@SerPounce](https://forum.bubble.io/u/SerPounce)\
**Post date:** [August 10, 2020, 11:14am UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/4 "2020-08-10T11:14:41Z")

</div>

Add a text element that says “This city already exists”.  
Add a workflow event “when an input’s value is changed” and in the only when area add “do a search for” the city.  
Then add an action that shows the text element.  
Make your button not clickable when the text element is visible.

 ![Screen Shot 2020-08-10 at 7.12.52 AM](https://us1.discourse-cdn.com/bubble/original/3X/7/2/725bb4237c5cd9e826315c2c1e3ff8f9a6addb1d.png)

---

<div class="post-metadata">

**Author:** ![doklyne](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@doklyne](https://forum.bubble.io/u/doklyne)\
**Post date:** [August 10, 2020, 11:46am UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/5 "2020-08-10T11:46:52Z")

</div>

Thanks for your feedback !

1. I’ve created the text field
2. I went into Workflow tab and I’ve added an event “when an input’s value is changed”
3. I selected for “Element” the “Searchbox”
4. Then I wanted to set up the “Only when” but I’m facing the same problem as explained at top of my post : I don’t know how to configure properly this part…

![Capture d’écran 2020-08-10 à 13.43.37](https://us1.discourse-cdn.com/bubble/original/3X/3/9/39f8b90185aa4e0599e6d92a8268aa11e7e53ab7.png)

 ![Capture d’écran 2020-08-10 à 13.43.43](https://us1.discourse-cdn.com/bubble/original/3X/1/9/19b3ca668f7e775f5b31cacb4ad3d3dbbda326f8.png)

So my method is :

- “Only when”
- “Search for Cities name” (so I imagine it returns an array ?)
- Then I check if this array contains “This SearchBox’s value” (which is the name of the city, since when I save the city I use the same variable construction)

Thanks in advance

---

<div class="post-metadata">

**Author:** ![SerPounce](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/serpounce/32/62068_2.png) [@SerPounce](https://forum.bubble.io/u/SerPounce)\
**Post date:** [August 10, 2020, 1:10pm UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/6 "2020-08-10T13:10:47Z")

</div>

The 2 types you are comparing must be the same. So you’ll need to have the search for cities return text values.

---

<div class="post-metadata">

**Author:** ![doklyne](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@doklyne](https://forum.bubble.io/u/doklyne)\
**Post date:** [August 11, 2020, 6:06am UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/7 "2020-08-11T06:06:31Z")

</div>

Hi,

Thanks for your feedback. There are several cities in my database ; I need to compare my search with the database to see if it contains the searched city.

I can’t compare the searched city to 1 value (string) only. I should check if the db (like an array) contains it.

I don’t understand your solution of comparing same kinds of values since there is an array (db) and a text (str in search)

I’m sure my function is good. I don’t understand how it can be false. I need more help.

1. In the “Only when” condition I have put “Search for Cities” (The Type is Cities and the constraint is “name”)
2. Then I put “contains” and I selected “This SearchBox’s value”

So my statement is “search in the cities list if the searchbox’s value already exist as a name”. The “contains” condition is made for this kind of purpose.

 ![Capture d’écran 2020-08-11 à 10.35.27](https://us1.discourse-cdn.com/bubble/original/3X/1/a/1a308866ece84cd2eed0c97aec033542eeacac9a.png)

Thanks in advance,

Thomas

---

<div class="post-metadata">

**Author:** ![doklyne](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@doklyne](https://forum.bubble.io/u/doklyne)\
**Post date:** [August 11, 2020, 2:32pm UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/8 "2020-08-11T14:32:20Z")

</div>

I found the solution. Finally.

So the best seems to be this:

1. In Workflow clicks on **Click here to add an event**
2. Select the **Elements** \> **An element is clicked**
3. Then in the window select for **Element** the _Button Validate the city_
4. Add a **Only when** condition which starts with _Do a search for_
5. In the side window select as **Type** _Cities_ ; don’t add any constraint
6. Click on **More…** in the **Only when** and select _'s name_
7. Click on **More…** in the **Only when** and select _doesn’t contain_
8. Click on **More…** in the **Only when** and select _SearchBox Search a city…_
9. Click on **More…** in the **Only when** and select _'s value_
10. Click on **More…** in the **Only when** and select _'s formatted address_

 ![when-condition](https://us1.discourse-cdn.com/bubble/original/3X/4/2/42516bde8b832cdd716ca32b8b42e6d0eaf5d851.png)

**Then create Steps in your workflow:**

1. Create a new City (as shown at the beginning of this FAQ post) but for name have _SearchBox Search a city…'s value’s formatted address_ instead of _SearchBox Search a city…'s value_
2. Add a clear inputs if you’d like

 ![create-city](https://us1.discourse-cdn.com/bubble/original/3X/b/e/be0daa97d92b8556097cef3123c3639de4c217b7.png)

---

<div class="post-metadata">

**Author:** ![amendezb](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@amendezb](https://forum.bubble.io/u/amendezb)\
**Post date:** [February 17, 2021, 11:13pm UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/9 "2021-02-17T23:13:51Z")

</div>

Your solution works like a charm.

The problem now is, how do I tell to the user that it hasn’t been created because it was duplicated?

Thanks!

---

<div class="post-metadata">

**Author:** ![asiruayomide](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@asiruayomide](https://forum.bubble.io/u/asiruayomide)\
**Post date:** [September 4, 2024, 11:56am UTC](https://forum.bubble.io/t/database-prevent-adding-existing-value/105599/10 "2024-09-04T11:56:02Z")

</div>

Use an alert to communicate that , if search for cities contain search-box value, show an alert that the searchbox name already exists .
