3

I am just starting to work with Cassandra in python using cqlengine.

I tried following this link and tried ran this script:

from cqlengine import columns
from cqlengine import Model
from cqlengine import connection

from cqlengine.management import sync_table

import uuid


class ExampleModel(Model):
    example_id = columns.UUID(primary_key=True, default=uuid.uuid4)
    example_type = columns.Integer(index=True)
    created_at = columns.DateTime()
    description = columns.Text(required=False)


connection.setup(['127.0.0.1'], 'cqlengine')

sync_table(ExampleModel)

But it throws up this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/zopper/Desktop/django-cassandra/local/lib/python2.7/site-packages/cqlengine/management.py", line 92, in sync_table
    keyspace = cluster.metadata.keyspaces[ks_name]
KeyError: 'cqlengine'

My pip freeze is:

Django==1.7.3
argparse==1.2.1
blist==1.3.6
cassandra-driver==2.1.3
cqlengine==0.21.0
django-cassandra-engine==0.2.1
djangotoolbox==1.6.2
futures==2.2.0
six==1.9.0
wsgiref==0.1.2

Please help me understand and solve this issue. Thanks.

Community
  • 1
  • 1
reza.safiyat
  • 499
  • 10
  • 21

2 Answers2

4

This was overlooked on my end - I'm fixing it now. create_missing_keyspace would rarely "do the right thing", and it's very difficult and time consuming to fix a keyspace created with the wrong parameters. You must now explicitly create a keyspace with the parameters you want.

Jon Haddad
  • 766
  • 6
  • 17
  • So, I reinstall the package from source? From GitHub? – reza.safiyat Jan 16 '15 at 18:45
  • 1
    You need to create the keyspace manually using create_keyspace(). create_missing_keyspace argument on sync_table is gone. I've updated the readme: https://github.com/cqlengine/cqlengine/blob/master/README.md – Jon Haddad Jan 16 '15 at 23:36
1

Edit: The create_missing_keyspace is ignored in cqlengine 0.21, but not 0.20. Try a version < 0.21 of cqlengine or manual creation (see below).

Create a keyspace like this:

cqlengine.management.create_keyspace("cqlengine", replication_factor=1, strategy_class="SimpleStrategy")

The available strategies are SimpleStrategy and NetworkTopologyStrategy.

I couldn't find updated docs for 0.21 so I checked the source. Here's the declaration of create_keyspace in 0.21:

def create_keyspace(name, strategy_class, replication_factor, durable_writes=True, **replication_values):
    """
    creates a keyspace
    :param name: name of keyspace to create
    :param strategy_class: keyspace replication strategy class
    :param replication_factor: keyspace replication factor
    :param durable_writes: 1.2 only, write log is bypassed if set to False
    :param **replication_values: 1.2 only, additional values to ad to the replication data map
    """
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
  • Though the documentation shows the function takes only one argument `name`, I am getting this error: Traceback (most recent call last): File "", line 1, in TypeError: create_keyspace() takes at least 3 arguments (1 given) – reza.safiyat Jan 16 '15 at 13:05
  • I also tried `sync_table(ExampleModel, create_missing_keyspace=True)`, but the same error arises. And the documentation says, `create_missing_keyspace=True` is default. – reza.safiyat Jan 16 '15 at 13:09
  • Yeah, I read some source now and the documentation is confusing. Especially since it's outdated :) – André Laszlo Jan 16 '15 at 13:09
  • But its the same version!!! They wouldn't change the source and let the docs remain unchanged, would they? – reza.safiyat Jan 16 '15 at 13:10
  • I guess that's why you should have at least the API reference automatically generated. Updating your documentation is easy to forget. – André Laszlo Jan 16 '15 at 13:19
  • https://github.com/cqlengine/cqlengine/blob/master/cqlengine/management.py#L21 And https://github.com/cqlengine/cqlengine/blob/master/cqlengine/management.py#L88 – reza.safiyat Jan 16 '15 at 13:22
  • 1
    The `create_missing_keyspace` argument [was removed](https://github.com/cqlengine/cqlengine/commit/4bb8b313c0d684b286904cbe4ac962fdf73b5dd1) only two days ago. It's still there in the documentation however. I posted [an issue on GitHub](https://github.com/cqlengine/cqlengine/issues/323). It's still there in 0.20, so you might want to try that version. – André Laszlo Jan 16 '15 at 13:35