与Python中的.where()一起使用时,带空格的Firestore文档字段名称返回错误 [英] Firestore document field names with spaces return an error when used with .where() in Python
问题描述
使用Firebase的Cloud Firestore在Python 3.7中编写一个非常简单的程序.在程序中,我使用.where()来提取集合的一部分,然后使用for循环对其进行迭代.当任何具有空格的字段名称传递到.where()时,在碰到循环时都会引发错误(google.api_core.exceptions.InvalidArgument: 400 Invalid property path "Insert field name here"
).
Writing a really simple program in Python 3.7 using Firebase's Cloud Firestore. In the program I use .where() to pull down part of a collection and then iterate through it with a for loop. When any field name that has a space is passed into the .where() it throws an error (google.api_core.exceptions.InvalidArgument: 400 Invalid property path "Insert field name here"
) when it hits the loop.
考虑到我既可以使用这些字段名输入数据,又可以提取数据,我认为这应该可行,只是不要将.where()与它们一起使用.我究竟做错了什么?只是不支持空格吗?
I would think this should work, considering that I can both put data up and pull data down with these field names, just not use .where() with them. What am I doing wrong? Are spaces just not supported?
这是我正在使用的代码的简化版本:
Here is a cut down version of the code I'm working with:
import firebase_admin
from firebase_admin import credentials, firestore
cred = credentials.Certificate(cert path)
applet = firebase_admin.initialize_app(cred)
db = firestore.client()
main_collection = db.collection(u'Referral_DB')
main_collection_obj = main_collection.where(u'English Name', u'==', u'Jerry').get()
for ref in main_collection_obj:
pass
推荐答案
在文档的限制"部分中发现,空格必须由反引号转义.
Found in the limitations section of the documentation that the spaces need to be escaped by backticks.
这有效:
main_collection_obj = main_collection.where(u'`English Name`', u'==', u'Jerry').get()
这篇关于与Python中的.where()一起使用时,带空格的Firestore文档字段名称返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!