Skip to content

Commit

Permalink
Merge pull request #74 from sethdenner/fix-foreign-key-field-creation
Browse files Browse the repository at this point in the history
Foreign Key Fields Not Created Properly In Meta Class
  • Loading branch information
sethdenner committed May 17, 2016
2 parents 02437b3 + 8e4e5fe commit 60a047e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
15 changes: 13 additions & 2 deletions djangocassandra/db/backends/cassandra/compiler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import re
import itertools

from django.db.utils import (
ProgrammingError
)

from django.db.models import ForeignKey
from django.db.models.sql.constants import MULTI
from django.db.models.sql.where import (
WhereNode,
Expand Down Expand Up @@ -389,9 +391,18 @@ def order_by(
for partition_key in self.partition_columns:
found = False
for filter_tuple in self.filters:
if partition_key == filter_tuple[0].name:
field = filter_tuple[0]
if isinstance(field, ForeignKey):
partition_key = re.sub(
'_id$',
'',
partition_key
)

if partition_key == field.name:
found = True
break
continue

if not found:
partition_key_filtered = False
break
Expand Down
2 changes: 1 addition & 1 deletion djangocassandra/db/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_cql_column_type(field):
internal_type = field.get_internal_type()
if internal_type == 'ForeignKey':
internal_type = (
field.related.model._meta.pk.get_internal_type()
field.rel.to._meta.pk.get_internal_type()
)

return internal_type_to_column_map[
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='djangocassandra',
version='0.4.5',
version='0.4.6',
description='Cassandra support for the Django web framework',
long_description=(
'The Cassandra database backend for Django has been '
Expand Down
18 changes: 18 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,21 @@ class Cassandra:
created = DateTimeField(
default=datetime.datetime.utcnow
)


class ForeignPartitionKeyModel(ColumnFamilyModel):
class Cassandra:
partition_keys = [
'related'
]
clustering_keys = [
'created'
]

related = ForeignKey(
ClusterPrimaryKeyModel,
primary_key=True
)
created = DateTimeField(
default=datetime.datetime.utcnow
)
52 changes: 51 additions & 1 deletion tests/test_columnfamily.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import warnings
from random import randint
from unittest import TestCase

from .models import (
ColumnFamilyTestModel,
ColumnFamilyIndexedTestModel
ColumnFamilyIndexedTestModel,
ClusterPrimaryKeyModel,
ForeignPartitionKeyModel
)

from .util import (
Expand Down Expand Up @@ -159,3 +162,50 @@ def test_partial_inefficient_get_query(self):

self.assertIsNotNone(partial_inefficient_get)
self.assertTrue(partial_inefficient_get.pk in self.cached_rows.keys())


class ForeignPartitionKeyModelTestCase(TestCase):
def setUp(self):
import django
django.setup()

self.connection = connect_db()

create_model(
self.connection,
ClusterPrimaryKeyModel
)
create_model(
self.connection,
ForeignPartitionKeyModel
)

def tearDown(self):
destroy_db(self.connection)

def test_order_by_efficient(self):
rel_instance = ClusterPrimaryKeyModel()
rel_instance.auto_populate()
rel_instance.save()

instances = []
for i in xrange(10):
instances.append(ForeignPartitionKeyModel.objects.create(
related=rel_instance
))

with warnings.catch_warnings(record=True) as w:
ordered_query = ForeignPartitionKeyModel.objects.filter(
related=rel_instance
).order_by('-created')

results = list(ordered_query)

self.assertEqual(
0,
len(w)
)
self.assertEqual(
10,
len(results)
)

0 comments on commit 60a047e

Please sign in to comment.