How to create records only if they don't already exist?

Hi all,

So my app has a map that I want to be filtered by several options. State, City, etc.

In order to do this, I need separate Collections for these options.

On the user signup page I have them enter the address in separate fields such as Street Address, City, Zip Code, etc.

How do I have Adalo enter their City in the Cities collection (“Create”), but only if it doesn’t already exist?

There’s no option in the “Sometimes…” settings for comparing what a user has entered in an input field to what exists in a Collection as far as I can tell.

Is there another way to accomplish this? I don’t want to have to set up some kind of notification system to notify me every time a user signs up to go and add their City to the City collection so the map functions properly…

Hi @naossoan,

You need to set up the condition differently:

  • Create City, Sometimes, when
  • Cities → Count
  • Add a filter (e.g. Name equals to CityInput)
  • equals to 0.

This will count the number of cities in the collection, filtered by name which is equal to input’s value. And if Count is 0 (meaning there are no cities with such name), then you can create a new City.

NB: “Equals” is case-sensitive, so New-York and new-york will be different cities. You can use “contains” instead (which is case-insensitive), but that will restrict from creating new city if there is an existing one with same letters in the middle.

Best,
Victor.

2 Likes

Oh wow yes that did work. Thank you!

I didn’t know “count” could be used in that way. I guess I just never noticed the filtering before.

I tested this out and it did work using “contains” and typing in the name of the city in lower case did not add the city to my database when it already existed with upper case first letter.

while I would prefer not to have the cities in lower case, I could get around errors in creation using either “is equal to” or “contains” (either duplicate entries based on case sensitivity or not creating a new entry when it should) by using lowercase and “is equal to” then I could be reasonably sure I wouldn’t get duplicate entries unless someone makes a typo.

Now I just have to figure out how to make the Map component not so useless :smiley:

1 Like

Any idea why this wouldn’t be working in some cases?

For example, on the user profile dashboard I give them the ability to update their username or email address, but I want to check to see if that already exists in the database, as well as pop up a modal saying the information was changed, or the username already exists.

So, on the button to submit the change, I set up a Link to the “already exists” modal to be > “sometimes…”

All Users > Count
Username > is equal to > inputUsername
“is not equal to”
0

Meaning, if there IS a username of that value (Count will not be 0) then pop up the modal saying the username already exists.

The problem is that the first time I click the button, nothing happens, but when I click the button again, the modal pops up.

So it’s as if it’s one step behind?

I tried doing this by making the button set a “variable” within the Logged In User to be the value of the input, then check against the variable instead of the input but the same thing happens.

Any ideas?

So I just confirmed this by doing the following:
Set the button to update the logged in user’s “variable” to the result of All Users > Count > Username is equal to inputUsername

put a text field on the page with the magic text of the logged in user’s variable field.

The first time I click the button after typing in a username I know exists, the text field remains empty. When I click it a second time, it shows “1”

So it’s like it’s registering the value of the input field the first time I click the button, then doing the “count” the second time I click the button.

however, if I just tell the button to assign a value to the text field, it does it immediately upon the first click.

What the heck is going on here!?!?

Hi @naossoan,

To start with: email uniqueness in Users’ collection is checked automatically.

Reg. your question - seems that something is wrong in the setup. May be you first update username (so modal doesn’t appear), and then on 2nd click as username is updated, modal “such username exists” appears.

This could explain this 2nd click behaviour.

Best,
Victor

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.