Append linked record using API?

Hello! I have two linked tables, let’s say Users and Events, and the Events table contains an Attendees column that links to Users. I want to add a new value to this column using the API. The code is quite simple:

await eventTable.update(eventId, { Attendees: [userId] });

But this overwrites the previous attendees. Can I just add one new value to the cell without overwriting the previous ones? I know I can read the previous values manually, add the new one and write the whole array back – but that’s a race condition waiting to happen, isn’t it? Is it possible that someone changes the value after I read the previous contents and before I write the new, augmented value?

This is what you have to do.

Yes, it is possible to have a race condition. However, there is no other option.

1 Like

@zoul - I have no idea if this will get rid of your race condition, but a better way to add to an existing array is to use the spread operator.

From the docs:

It is commonly used when you want to add a new item to a local data store, or display all stored items plus a new addition. A very simple version of this kind of action could look like so:

let numberStore = [0, 1, 2];
let newNumber = 12;
numberStore = [...numberStore, newNumber];

It will not really affect the possibility of a race condition. You are still reading the previous value and adding the new one before writing the whole array back. This will not affect the fact that someone else could have updated the field in between the read and the write. At best, it will slightly decrease the time between the read and the write, but it is impossible to reduce that time to zero.

On the other hand, given how most Airtable bases are used, it is unlikely that this will be a problem in most situations. If this type of split second timing important, Airtable may not be the right platform.

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