0

我在 python 中有以下类,并使用 cqlengine 作为 cassandra DB 的对象映射器,

class Company(Model):
    id = columns.Integer(index=True, primary_key=True)
    company_name = columns.Text(required=False)
    created_at = columns.DateTime(default=datetime.now)

class Social(Model):
    id = columns.Integer(index=True, primary_key=True)
    name = columns.Text(required=False)

class UserCompany(Model):
    id = columns.Integer(index=True, primary_key=True)
    user_id = columns.Integer(required=True)
    company_id = columns.Integer(required=True)

array_of_tables=[UserCompany, Company, Social]
    for table in array_of_tables:
        sync_table(table)

问题:执行代码时,我无法在 cassandra 中创建两个以上的表。

错误信息:

  File "test.py", line 109, in <module>
    create_tables()
  File "test.py", line 68, in create_tables
    sync_table(table)
  File "/usr/lib64/python2.6/site-packages/cassandra/cqlengine/management.py", line 259, in sync_table
    execute(qs)
  File "/usr/lib64/python2.6/site-packages/cassandra/cqlengine/connection.py", line 171, in execute
    result = session.execute(query, params, timeout=timeout)
  File "cassandra/cluster.py", line 1602, in cassandra.cluster.Session.execute (cassandra/cluster.c:27334)
  File "cassandra/cluster.py", line 3347, in cassandra.cluster.ResponseFuture.result (cassandra/cluster.c:65206)
cassandra.InvalidRequest: code=2200 [Invalid query] message="Cannot create secondary index on partition key column id"

需要您帮助解决此问题。

4

1 回答 1

0

不允许为主列设置索引。我刚刚从代码中删除了它并且它起作用了。它应该如下所示。在这里,我只是使用了 UUID 而不是整数,这仍然与实际问题无关。

class Company(Model):
    id = columns.UUID(default=uuid.uuid1(), primary_key=True))
    company_name = columns.Text(required=False)
    created_at = columns.DateTime(default=datetime.now)

class Social(Model):
    id = columns.UUID(default=uuid.uuid1(), primary_key=True)
    name = columns.Text(required=False)

class UserCompany(Model):
    id = columns.UUID(default=uuid.uuid1(), primary_key=True)
    user_id = columns.Integer(required=True)
    company_id = columns.Integer(required=True)

array_of_tables=[UserCompany, Company, Social]
    for table in array_of_tables:
        sync_table(table)
于 2015-11-05T15:08:34.267 回答