Python Scripts using the base.add_link() function

A link_id is the unique internal identifier SeaTable assigns to each Link-type field when you create it between two tables. It’s something like the “technical name” of that relationship, distinct from the name you give the field yourself (for example, the field is called “Products” for the human, but internally SeaTable identifies it with a code like ‘MYtg’ or similar).

When you write a Python script that needs to create a relationship between two rows in different tables — using the base.add_link() function — SeaTable doesn’t accept the visible field name (“Products”), it needs that exact internal code to know which relationship you’re referring to. It’s like the difference between calling someone by their nickname (works when talking to humans) vs. needing their exact ID number for an official procedure (what the system requires).

That’s why you look it up with the metadata script: get_metadata() returns the complete list of fields for a table, and for the ones that are type link it includes that real link_id inside col[‘data’]. Without that data, base.add_link() fails or doesn’t know which relationship to create.


Hi @Rafael,
You’re right, the several avaimable functions to create/update link(s) need the column’s link_id. However, there’s a much easier way to find it using base.get_column_link_id(table_name, column_name).
Feel free to consult the developer documentation to get some more information about linking - or to share the link with any AI agent you may use to help you creating scripts.

Bests,
Benjamin

I was using this and it worked, but it was very long.

from seatable_api import Base, context
server_url = context.server_url or ‘https://cloud.seatable.io
api_token = context.api_token or ‘TU_TOKEN’
base = Base(api_token, server_url)
base.auth()
metadata = base.get_metadata()
print(“Tablas encontradas:”)
for table in metadata[‘tables’]:
print(’ -‘, repr(table[‘name’]))
print()
print(“Columnas de NombreTabla:”)
for table in metadata[‘tables’]:
if table[‘name’].strip() == ‘NombreTabla’:
for col in table[‘columns’]:
print(’ -‘, col[‘name’], ‘| type:’, col[‘type’])
if col[‘type’] == ‘link’:
print(’ data:', col.get(‘data’))

Thanks