October 25, 2024
Chicago 12, Melborne City, USA
python

Fauna DB – how to create, in python, doc in a collection with refs to docs in other collections?


I have a collection in Fauna DB with this schema:

collection yalofiles {
  yaloid: Ref<yalos>
  yaloContentId: String
  name: String
  *: Any
}

where yaloid contains a reference to a document in another collection. I am trying to create a yalofile as described in Fauna documentation here in python. My settings are:

  • Python version: 3.12.5
  • fauna 2.3.0 from PyPi (Official Python Driver for Fauna v10)

The code mimics the documentation (removed try / except’s):

    def insertYalofile(self, yalofile, yaloId):
       qyalo = fql(f'{YALOS_COLLECTION}.byId({yaloId})')
       ryalo: QuerySuccess = self._client.query(qyalo)
       yalofile["yaloid"] = ryalo.data  # yalo Document
       query = fql(f'{YALOFILES_COLLECTION}.create({yalofile})')
       res: QuerySuccess = self._client.query(query)  # This query FAILs with the error described below
       return res.data

But the second query returns an error that reports the following when catch’d:

400: invalid_query The query failed 1 validation check
--- 
error: Expected `)` or `,` at *query*:1:40   
  |
1 | yalofiles.create({'yaloid': Document(id='412701900943655120',coll=Module(name="yalos"),ts=datetime.datetime(2024, 10, 25, 7, 40, 53, 280000, tzinfo=datetime.timezone.utc),data={'name':'alfred stevens','yalotype':'artist','displayname':'Alfred Stevens','owner':'yaluba','artist':'412652432707813579','tags':['412472715978473675']}), 'yaloContentId': '66f5bbd2bff7b6328f8fb52a', 'name': 'alfred stevens', 'isMain': True, 'mime': 'image/jpeg', 'provider': 'cloudflare', 'stosol': 'r2'})
  |                                        ^
  |

Looks like yalofile["yaloid"] is not expected to contain a Document but something else. The documentation of fauna 2.3.0 has a class for DocumentReference but I can’t find anywhere how to use it and get this to work.

How can I create in python a document in a collection that contains a reference to a document in another collection?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video