pythonfaunadb

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:

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?

-- ANNEX --

After posting, I tried with

yalofile["yaloid"] = models.DocumentReference(YALOS_COLLECTION, yaloId)' 

and it did not work (same error). But it works if the fql in the second query is built like this:

yalofilestr = '{ \'yaloid\': yalos(' + yaloId + '), ' + repr(yalofile)[1:] 
yalofilestr = yalofilestr.replace("True", "true") ;
yalofilestr = yalofilestr.replace("False", "false")
query = fql(f'{YALOFILES_COLLECTION}.create({yalofilestr})')

This is definitely not nice at all. For the fql query, the whole object needs to be string-ified correctly. Even boolean values need to be converted. There must be a better way to do this in Python!!!


Solution

  • Passing the ryalo.data back in won't work since it's rendered with those Document and DocumentReference that you're seeing, which isn't valid FQL.

    The simple way to do this is to combine both queries into one, which cuts down latency and is the preferred way in Fauna. So you can do something like this FQL:

      let yalo = yalos.byId(<your id>)
      yalofiles.create( {'yaloid': yalo), ... })
    

    Or even just:

      yalofiles.create( {'yaloid': yalos.byId(<your id>), ... })