firebase - access firestore database other than (default) in python - Stack Overflow

I cannot access a Google Firestore database other than one named "(default)". I looked other

I cannot access a Google Firestore database other than one named "(default)". I looked other other solutions online, and added a key to my credential "databaseId" but that doesn't work. Here is my current script

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)

It still only puts data in the (default) data base and if that database doesn't exist, throws me an error. I checked through the Google CLI and the other database does exist.

I cannot access a Google Firestore database other than one named "(default)". I looked other other solutions online, and added a key to my credential "databaseId" but that doesn't work. Here is my current script

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)

It still only puts data in the (default) data base and if that database doesn't exist, throws me an error. I checked through the Google CLI and the other database does exist.

Share Improve this question edited Jan 17 at 19:29 Doug Stevenson 319k36 gold badges456 silver badges473 bronze badges asked Jan 17 at 19:21 tomtom 1,0774 gold badges16 silver badges34 bronze badges 1
  • Did you see stackoverflow/questions/78533417/…? – Frank van Puffelen Commented Jan 17 at 19:33
Add a comment  | 

1 Answer 1

Reset to default 1

Based on issue #818 in the Firebase Admin SDK version 6.6 or later it seems that you can specify the database_id when creating the client with:

db = firestore.client(database_id="your database id")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745346989a4623596.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信