Can't override partition key settings when subclassing a Model

Description

When subclassing a Model changes to the partition_key are not working because of the way the code handles the column definition processing.

Example:

 

Running this code raises an exception:

{{Traceback (most recent call last):
File "/home/bobh/test.py", line 10, in <module>
class B(A):
File "/home/bobh/.virtualenvs/traffica_stc/lib/python3.7/site-packages/cassandra/cqlengine/models.py", line 977, in _new_
raise ModelException("clustering_order may be specified only for clustering primary keys")
cassandra.cqlengine.models.ModelException: clustering_order may be specified only for clustering primary keys}}

because in ModelMetaClass (models.py:920) the code transforms the column definitions using the combined list of columns - inherited columns + column_definitions, instead of using the defined_columns OrderedDict() which merges the attributes properly.

CHanging line 920 from:

for k, v in column_definitions:

to
for k, v in defined_columns.items():

resolves the problem and allows the attributes of the columns to be processed properly.

Environment

None

Pull Requests

None

Activity

Show:
Alan Boudreault
October 30, 2019, 12:31 AM

Hi Bob,

This is not something supported in the Object mapper. I´d suggest to not use model inheritance for different CQL tables... this can lead to undesired issues. Model inheritance is convenient to query the same CQL table:

https://docs.datastax.com/en/developer/python-driver/3.20/cqlengine/models/#model-inheritance

If you want to share some model field definitions, I think you could probably do something like (not tested):

Not a Problem

Assignee

Unassigned

Reporter

Bob Haddleton

Fix versions

None

Labels

Reproduced in

None

PM Priority

None

External issue ID

None

Doc Impact

None

Reviewer

None

Size

None

Pull Request

None

Affects versions

Priority

Major