gtalarico / pyairtable

Python Api Client for Airtable
https://pyairtable.readthedocs.io
MIT License
765 stars 138 forks source link

Issue with creating multipleRecordLinks '0.options.isReversed is missing' #368

Closed Ledoux closed 4 months ago

Ledoux commented 5 months ago

Hi I want to create a link content.account from a 'contents' table towards the associated account (via the content.accountId field).

When I try to create the link and the reverse link :

linked_field = accounts_table.create_field(
    'contents', 
    'multipleRecordLinks', 
    options={
        'linkedTableId': contents_table.id
    }
)

 contents_table.create_field(
      'account', 
      'multipleRecordLinks', 
      options={
          'inverseLinkFieldId': linked_field.id,
          'linkedTableId': accounts_table.id,
          'prefersSingleRecordLink': True
     }
)

I cannot, I have such issues :

requests.exceptions.HTTPError: ('422 Client Error: Unprocessable Entity for url: https://api.airtable.com/v0/meta/bases/appYDDFYw4ZmrGAa0/tables/tbl3s3YftIiu3G8X5/fields', "{'type': 'INVALID_FIELD_TYPE_OPTIONS_FOR_CREATE', 'message': 'Invalid options for contents.account: Failed schema validation: account.0.options.isReversed is missing, prefersSingleRecordLink is not included in the account.1.options schema'}")

I tried a bit to add all the permutation possibles with isReversed, and inverseLinkFieldId but I m stuck,it would be cooll to have a better understanding of this api please, thanks a lot:)

Best,

BAPCon commented 4 months ago

https://community.airtable.com/t5/development-apis/creating-a-table-with-multiplerecordlinks-field-with-rest-api/m-p/157602#M12761

I can't find any working examples of this functionality even from the REST API, in the mean time I would suggest using lookup tables with one field being the primary field:


linked_field1 = table1.create_field(
    'contents', 
    'multipleRecordLinks', 
    options={
        'linkedTableId': table2.id,
    }
)

linked_field2 = table2.create_field(
      'accounts', 
      'multipleRecordLinks', 
      options={
        'linkedTableId': table1.id,
     }
)```
mesozoic commented 4 months ago

Reading the API documentation for multipleRecordLinks, it looks like the only options they support for creating or updating fields are linkedTableId and viewIdForRecordSelection. The inverseLinkFieldId and prefersSingleRecordLink properties are only available when reading the field metadata.

My understanding of how these fields work is that creating a link field from table1→table2 should automatically create the reciprocal field on table2, which you'd be able to identify by looking at the inverseLinkFieldId property. For example:

>>> created_field = table1.create_field('link', 'multipleRecordLinks', options={'linkedTableId': table2.schema().id})
>>> created_field
MultipleRecordLinksFieldSchema(type='multipleRecordLinks', options=MultipleRecordLinksFieldOptions(is_reversed=False, linked_table_id='tbl...', prefers_single_record_link=False, inverse_link_field_id='fldVIhHyCq5yUbWDb', view_id_for_record_selection=None), id='fld9GFYJfZo8hk1UG', name='link', description=None)
>>> inverse = next(
...     field for field in table2.schema(force=True)
...     if field.type == "multipleRecordLinks"
...     and field.options.inverse_link_field_id = created_field.id
... )
>>> inverse
MultipleRecordLinksFieldSchema(type='multipleRecordLinks', options=MultipleRecordLinksFieldOptions(is_reversed=False, linked_table_id='tbl...', prefers_single_record_link=False, inverse_link_field_id='fld9GFYJfZo8hk1UG', view_id_for_record_selection=None), id='fldVIhHyCq5yUbWDb', name='One', description=None)

I'm going to close this issue since it's not a bug with the pyairtable library. I suggest asking on the Airtable Community Forums if you have follow-up questions or feature requests.