I would like to insert this data: [{ name: 'Paris'...
# help
l
I would like to insert this data: [{ name: 'Paris', country: France }, { name: 'Tokyo', country: Japan }] But my table has a country_id column as a foreign key. So i will need to do this: const { data, error } = await supabase .from('cities') .insert([ { name: 'Paris', country_id: 1 }, { name: 'Tokyo', country_id: 2 } ]) How can i get the the country_id to insert the data?
n
Hello @Lesourdingo! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
g
Normally I would have the user input for such things be a select that was built on a database call to get name/id pairs and then use the id. Or if a text input I would be doing partial text ilike look up for a match or a validation look up on full name and get the id there.
n
Lesourdingo (2022-04-07)
l
its not an input, it is a big json file with many rows
g
You could also write a rpc Postgres function to do a select to get the ids based on name as part of the insert, but it is still multiple accesses to the db, just one API call. But still have to deal with what if name is not there.
On js side you would need to do a select to get id, or one select for all id/name pairs and then use the in memory table to replace the names with ids as you build your insert.
l
so for each row i will need to do a select request to get id and then insert it ?
g
Or do the one big select and you have a conversion table to use in a loop on the name field. This is probably best way for lots data.
l
oh yeah i see, that better maybe
g
It will be much, much faster than individual selects thru the api.
l
ok
thanks for help