[Python] Fauna DB - how to create, in python, doc in a collection with refs to docs in other...

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024.

  1. Stack

    Stack Membro Participativo

    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?

    -- 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!!!

    Continue reading...

Compartilhe esta Página