访问除Python(默认)之外的firestore数据库

问题描述 投票:0回答:1

除了名为“(默认)”的数据库外,我无法访问其他 Google Firestore 数据库。我在线查看了其他解决方案,并为我的凭据“databaseId”添加了密钥,但这不起作用。这是我当前的脚本

def firestore_add_doc(data):
    print('DEBUG Firestore document creation script triggered')

    # Load credentials from dictionary
    cred = credentials.Certificate(cred_dict)

    # Firestore collection name (must exist)
    FIRESTORE_COLLECTION = "firestore_collection_one"

    try:
        # Check if Firebase is already initialized
        if not firebase_admin._apps:
            firebase_admin.initialize_app(cred, {
                'projectId': 'cheftest-f174c',
                'databaseId': 'cheftestfirestore'
                })

        else:
            print("Firebase already initialized.")
        
        # Get Firestore client
        db = firestore.client()

        # Add document to Firestore
        collection_ref = db.collection(FIRESTORE_COLLECTION)
        doc_ref = collection_ref.add(data)
        print(f"Document added with ID: {doc_ref[1].id}")

    except Exception as e:
        print(f"Error adding document to Firestore: {e}")

if __name__ == "__main__":
    data = {
        "key1test": "value1test",
        "key2test": "value2test"
    }
    firestore_add_doc(data)

它仍然只将数据放入(默认)数据库中,如果该数据库不存在,则会抛出错误。我通过 Google CLI 检查过,另一个数据库确实存在。

python firebase google-cloud-firestore firebase-admin
1个回答
0
投票

基于 Firebase Admin SDK 中的 issue #818,您似乎可以在创建

client
时指定 database_id:

db = firestore.client(database_id="your database id")
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.