2

I want create a table with two primary_key by it's django model as below:

class UserView(Model):
    email= columns.Text(primary_key=True)
    entryLink= columns.Text(primary_key=True)
    date= columns.Date(default=datetime.date.today())

but when I want create table as below:

>>> from cqlengine import connection
>>> from cqlengine.management import create_table
>>> from MainAPP.models import UserView
>>> connection.setup(['127.0.0.1:9160'])
>>> create_table(UserView)

I see this error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "H:\Web-Programming\Python\Project\Prexter\Virtual-Environment\Lib\site-packages\cqlengine\management.py", line 97, in create_table
execute(qs)
  File "H:\Web-Programming\Python\Project\Prexter\Virtual-Environment\Lib\site-packages\cqlengine\connection.py", line 172, in execute
return connection_pool.execute(query, params)
  File "H:\Web-Programming\Python\Project\Prexter\Virtual-Environment\Lib\site-packages\cqlengine\connection.py", line 164, in execute
    raise CQLEngineException(unicode(ex))
CQLEngineException: Bad Request: Missing CLUSTERING ORDER for column entryLink

When I remove primary_key property from entryLink field, I have no error! but I want define entryLink as a primary_key! What is my mistake?

Omid Ebrahimi
  • 1,150
  • 2
  • 20
  • 38

2 Answers2

1

A database table cannot have 2 primary key. If you are looking for a Composite Primary Key, Django does not support that yet.

Now, what you might be looking for is unique=True (a candidate key).

class UserView(Model):
    email= columns.Text(primary_key=True)
    entryLink= columns.Text(unique=True)
    date= columns.Date(default=datetime.date.today())

You can also read this post for a better understanding

Community
  • 1
  • 1
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • This is true! But note that my database is cassandra! in this code my first primary_key will set as a partition_key and other primary_keys must be my clustering keys! please read this section: [https://cqlengine.readthedocs.org/en/latest/topics/models.html#column-options](https://cqlengine.readthedocs.org/en/latest/topics/models.html#column-options) – Omid Ebrahimi Jul 12 '13 at 18:46
  • I still dont buy the fact that a table should have multiple `primary` keys. Candidate keys - YES, but `primary` key ?? Even if cassandra supports it, django does not. – karthikr Jul 12 '13 at 18:51
  • 1
    I do see `In CQL, there are 2 types of primary keys: partition keys and clustering keys. As with CQL, the first primary key is the partition key, and all others are clustering keys, unless partition keys are specified manually using partition_key` - Now, django would not support this by default. However, you can take a look at 3rd party apps like `django-nonrel` and see how it is implemented there. – karthikr Jul 12 '13 at 18:53
  • Thanks for your answer.as a result I have a serious truble with django! because cassandra isn't a relational database like MySQL, SqlServer, oracle,...! It's a no sql database which have a different structure completely! in cassandra we can define many primary_keys. you can se this example : [Cassandra Example](http://www.ebaytechblog.com/2012/07/16/cassandra-data-modeling-best-practices-part-1/) – Omid Ebrahimi Jul 12 '13 at 19:19
  • 1
    I got that. I have worked with non-relational databases, but never cassandra. I have not faced this issue. Sorry for not being of much help. I was talking about [django-nonrel](http://www.allbuttonspressed.com/projects/django-nonrel) which might offer some support (Have not worked on it myself though). – karthikr Jul 12 '13 at 19:26
1

I found My Answer! This is a funny bug! I have a capitalized character among entryLink field, and it caused considered error! This is so funny! because this applies to primary_key only! and it isn't true about other fields!!! primary_keys can't have a capitalized character among!

My modified code:

class UserView(Model):
    email= columns.Text(primary_key=True)
    link= columns.Text(primary_key = True)
    date= columns.Date(default=datetime.date.today())
Omid Ebrahimi
  • 1,150
  • 2
  • 20
  • 38
  • I think what you need is partition_key instead of multiple parimary_keys https://datastax.github.io/python-driver/api/cassandra/cqlengine/columns.html#cassandra.cqlengine.columns.Column.partition_key – Codious-JR Mar 22 '18 at 11:20