Creating primary index on collection using python

Hello,
I just started using python sdk to interact with couchbase db.
I could use cbq to create a primary index on collection as follows:
CREATE PRIMARY INDEX ON test.test_scope.test_collection;
where test is the bucket, test_scope is the scope and test_collection is the collection in my db.

However using python sdk with the following codes, I could not create the same primary index, maybe the syntax is incorrect, however I couldn’t find relevant instructions from the python couchbase documentation. Hopefully someone can point me to the correct direction to fix this out.

from couchbase.cluster import Cluster, ClusterOptions
from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import QueryOptions
from couchbase.management.queries import CreatePrimaryQueryIndexOptions

cluster = Cluster(
“couchbase://localhost”,
authenticator=PasswordAuthenticator(
“admin”,
“Passw9rd!”))
cb = cluster.bucket(“test”)
view_manager = cb.view_indexes()
query_index_manager = cluster.query_indexes()

query_index_manager.create_primary_index(’test.test_scope.test_collection’, CreatePrimaryQueryIndexOptions(ignore_if_exists=True))

@mzw can you please also share the logs and the error you are seeing? which python SDK version are you using?

Hello Daschl,
Just wonder how to check the python-couchbase sdk version?
for example with dash, I could check it by using

import dash
print (dash.version)

Seems like it’s not working for couchbase python sdk.

import couchbase
print(couchbase.version)

There is no error message when I run:

query_index_manager.create_primary_index(‘test.test_scope.test_collection’, CreatePrimaryQueryIndexOptions(ignore_if_exists=True))

However I don’t see the index being created in the server page

brgds,
Millie

but if I am using the cbq to create the primary index

I could see the index being created in the server page.

The create index python code actually look like this … back quote has been automatically transformed in the message post.

Hi @mzw - The Python SDK does not support index management for collections yet. See the ticket here for tracking the implementation of index management for collections.

In the interim, you can create a query to add indexes with collections. See an example below.

cluster.query(
    "CREATE PRIMARY INDEX ON `{}`.`{}`.{}".format("travel-sample","inventory","airport")).execute()

I hope this helps.