maxtepkeev / architect

A set of tools which enhances ORMs written in Python with more features
Other
391 stars 57 forks source link

Querying after partitioning returns an empty result #60

Open tahmidkhan opened 5 years ago

tahmidkhan commented 5 years ago

The project is running on Django. I set up my model as follows:

import architect
from django.contrib.postgres.fields import JSONField

@architect.install('partition', type='range', subtype='string_firstchars', constraint='10', column='collection')
class PartitionedDocument(models.Model):
    collection = models.CharField(max_length=10)
    content = JSONField(default=dict)

In other words, I am partitioning using the first 10 characters of the collection field. Plus, I ensure the collection is always a string of 10 characters (from a pre-determined list), so its a nice partitioning system. The list looks like this:

collection_labels = [
    '91IJAYXLPc',
    'kzMZFCHavP',
    'SMyJzyY9ze',
    'EAW95zQtED',
    'AzDWtgZ6MA',
    'Kv7wrKX9m3',
    'BO9CsGIqpd',
    'fQhDlqVNJs',
    'tvj1zF3Nmm',
    'IAPzdfsjFM',
    'hLuumRvCK4',
    'ErwIeGjMwE',
    'ebS3sy7xmJ',
    'aLyd8Hrcpe',
    '8hdhGCFmJU',
    'AfT7kLR0ep',
    '0MPS0KLsrT',
    'k17vDmktNB',
    'SluHmP1X2K',
    '9OMXJZJqHE'
]

Anyway, I added some data to my database after doing all this, and then ran a query like so:

PartitionedDocument.objects.annotate(
    full_name = RawSQL("(content->>'full_name')::text", []),
    age = RawSQL("(content->>'age')::int", [])
).filter(
    age__gte = 30
).extra(where=["lower(substr(collection, 1, 10)) = '91IJAYXLPc'"])

If everything went correctly, I should only be hitting the table that corresponds to the collection with a value of '91IJAYXLPc', and then I would filter based on the JSONField. I know for a fact that objects that fall under this condition exists in my database, however, the queryset returned is empty.

Did I do something wrong? Thanks.