cassandra 无法在cqlengine中使用sync_table()创建两个以上的表

ui7jx7zq  于 11个月前  发布在  Cassandra
关注(0)|答案(2)|浏览(163)

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

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"


需要你的帮助来解决这个问题。

20jt8wwn

20jt8wwn1#

不允许为主列设置索引。我只是从代码中删除了它,它工作了。它应该看起来像下面。在这里,我只是使用了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)

字符串

7eumitmz

7eumitmz2#

用户公司模型

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

字符串

公司型号

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

社会模式

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


在使用Cassandra时,使用UUID作为主键是一个很好的做法,可以更好地分发并避免集群中的热点。
在这些更改之后,您应该能够创建表,而不会遇到“Cannot create secondary index on partition key column”错误。
试试这个变化。

相关问题