简体   繁体   English

使用 Python 从 Firestore 加载文档时出现“错误的应用程序引用”

[英]"Wrong app reference" when loading documents from Firestore with Python

I have a database in a GCP project "A" that was created from a copy of another database in project "B".我在 GCP 项目“A”中有一个数据库,它是从项目“B”中的另一个数据库的副本创建的。 This was achieved with the Cloud Firestore managed export and import service from GCP.这是通过 GCP 的 Cloud Firestore 托管导出和导入服务实现的。

When trying to load documents from a specific collection from the database in project "A", an error is raised:尝试从项目“A”中的数据库加载特定集合的文档时,出现错误:

ValueError                                Traceback (most recent call last)
<ipython-input-16-935f61174138> in <module>()
      1 collection_docs = db.collection("<collection>").stream()
----> 2 for doc in collection_docs:
      3   print(doc.id)

5 frames
/usr/local/lib/python3.7/dist-packages/google/cloud/firestore_v1/_helpers.py in reference_value_to_document(reference_value, client)
    245     if document._document_path != reference_value:
    246         msg = WRONG_APP_REFERENCE.format(reference_value, client._database_string)
--> 247         raise ValueError(msg)
    248 
    249     return document

ValueError: Document 'projects/<project B>/databases/(default)/documents/<another collection>/<document>' does not correspond to the same database ('projects/<project A>/databases/(default)') as the client.

Note that <another collection> is referenced in documents from <collection>请注意, <another collection>在来自<collection>的文档中被引用

Why the reference keeps pointing to the old database?为什么引用一直指向旧数据库?

UPDATE: It turns out the initial export was correct and the references properly updated, but after that, while updating other fields using a buggy Python script, the references got unnecessarily and incorrectly "updated" to the old project.更新:事实证明初始导出是正确的并且引用已正确更新,但之后,在使用错误的 Python 脚本更新其他字段时,引用被不必要且错误地“更新”到旧项目。

The correct way to obtain this field would be:获取该字段的正确方法是:

db.document(<document reference>.path)

Still this question remains:这个问题仍然存在:

Is it possible to load the documents without bringing the reference?是否可以在不带参考的情况下加载文件?

Here is the Python snippet that produces the error:这是产生错误的 Python 片段:

cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
  'projectId': <project A>,
})

db = firestore.client()

collection_docs = db.collection("<collection>").stream()
for doc in collection_docs:
  print(doc.id)

You can use the Cloud Firestore managed export and import service to export all documents or just specific collections. The data exported from one Cloud Firestore database can be imported into another Cloud Firestore database across projects.您可以使用 Cloud Firestore 管理的导出和导入服务导出所有文档或仅导出特定的 collections。从一个 Cloud Firestore 数据库导出的数据可以跨项目导入到另一个 Cloud Firestore 数据库。 The Cloud Firestore managed export and import service is available through the gcloud command-line tool and the Cloud Firestore API ( REST , RPC ). Cloud Firestore 托管导出和导入服务可通过gcloud 命令行工具和 Cloud Firestore API( RESTRPC )获得。

Here is the documentation , explaining the same.这是文档,解释相同。

Key points to note when importing data:导入数据时需要注意的要点:

  • When you import data, the required indexes are updated using your database's current index definitions.导入数据时,所需的索引会使用数据库的当前索引定义进行更新。 An export does not contain index definitions.导出不包含索引定义。
  • Imports do not assign new document IDs.导入不会分配新的文档 ID。 Imports use the IDs captured at the time of the export.导入使用导出时捕获的 ID。 As a document is being imported, its ID is reserved to prevent ID collisions.在导入文档时,会保留其 ID 以防止 ID 冲突。 If a document with the same ID already exists, the import overwrites the existing document.如果已存在具有相同 ID 的文档,则导入会覆盖现有文档。

Also, I would like you to go through this answer where a community member has explained how to use a value from the Firestore DB of one primary project in order to query the Firestore DB of a secondary project using references.此外,我希望您通过这个答案转到 go,社区成员解释了如何使用一个主要项目的 Firestore 数据库中的值,以便使用引用查询辅助项目的 Firestore 数据库。 Try and see if you could make it work too without errors, as it is very much possible to point a document from another project in Firestore.尝试看看是否可以让它正常工作而不会出错,因为很可能会指向 Firestore 中另一个项目的文档。

UPDATE更新

I reproduced your case by creating two projects A and B. In my case, Project A has a Firestore database, with all the data which I am exporting to project B. I followed this documentation for moving the data between projects and it was successfully done.我通过创建两个项目 A 和 B 重现了您的案例。在我的案例中,项目 A 有一个 Firestore 数据库,其中包含我要导出到项目 B 的所有数据。我按照此文档在项目之间移动数据并成功完成. Now after this, I created a service account in Project B and gave the Owner, Service Account Token Creator permissions .在此之后,我在项目 B 中创建了一个服务帐户,并授予Owner, Service Account Token Creator permissions Generated a private key file and set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains the service account key like this:生成私钥文件并将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为包含服务帐户密钥的 JSON 文件的文件路径,如下所示:

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-file.json"

After that I wrote the below Python script which worked fine for me and it printed the document id of my Firestore database( which was exported from Project A and imported to Project B) without any errors.之后,我编写了以下 Python 脚本,它对我来说工作正常,它打印了我的 Firestore 数据库的文档 ID(从项目 A 导出并导入到项目 B),没有任何错误。

firebase.py firebase.py

import firebase_admin
from firebase_admin import auth,firestore
from firebase_admin import credentials
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
 'projectId': "projectB’sID",
})
db = firestore.client()
collection_docs = db.collection("collection-name").stream()
for doc in collection_docs:
 print(doc.id)
 

requirements.txt要求.txt

firebase_admin=5.0.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM