gtalarico / pyairtable

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

all(formula=match ...) doesn't seem to be working #145

Closed sschra01 closed 2 years ago

sschra01 commented 2 years ago

My code:

modMatch = ({"Name": args.Mod}) theRecords = theTable.all(formula=modMatch, fields=['Name']) for record in theRecords: print(modMatch) print(record) if record['fields']['Name'] == args.Mod: theMod = record['id']

This iterates through all records and does not only return a single record which matches the arg. Output: python3 genData.py --Mod "Spy Pack 1" {'Name': 'Spy Pack 1'} {'id': 'recUgCMdpp1iGjKpl', 'fields': {'Name': 'Spy Pack 1'}, 'createdTime': '2022-03-04T17:51:07.000Z'} {'Name': 'Spy Pack 1'} {'id': 'recw158z9uaJpyuFe', 'fields': {'Name': 'ImprovedCrewDog'}, 'createdTime': '2022-03-08T14:01:31.000Z'}

sschra01 commented 2 years ago

I went back to the API and found a solution, so maybe there's an update in documentation. These work:

modMatch = "{Name} = '" + args.Mod + "'" theRecords = theTable.all(formula=modMatch, fields=['Name'])

This is a linked field

modMatch = "{Mod} = '" + args.Mod + "'" theRecords = theTable.all(formula=modMatch)

gtalarico commented 2 years ago

modMatch = ({"Name": args.Mod}) theRecords = theTable.all(formula=modMatch, fields=['Name'])

A dictionary is not a valid formula value. A valid formula should be a string that is a valid Airtable formula expressions (eg. "{First Name}='John'").

The easiest way is to use the helper match() function which helps you create these formula strings:

>>> match({"First Name": "John", "Age": 21})
"AND({First Name}='John',{Age}=21)"
>>> match({"First Name": "John"})
"{First Name}='John'"
>>> match({"Registered": True})
"{Registered}=1"
>>> match({"Owner's Name": "Mike"})
"{Owner\'s Name}='Mike'"

Docs on match: https://pyairtable.readthedocs.io/en/latest/api.html#match