我在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中创建两个以上的表。
Erorr留言:
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"
需要您帮助解决此问题。
答案 0 :(得分: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)