Turns out there was a tiny class Meta at the very bottom of the very big 
model, obfuscating mine defining the index.. Something not even ChatGPT 
could imagine!

On Thursday, December 5, 2024 at 12:31:01 PM UTC+1 RANGA BHARATH JINKA 
wrote:

> Hi,
>
> The issue is that partial indexes with conditions (condition attribute in 
> models.Index) were introduced in *Django 3.2*, but they rely on the 
> database backend's ability to support them. PostgreSQL does support partial 
> indexes, so that part should be fine. However, your problem likely stems 
> from the following points:
> 1. *Condition on channel_type Value* 
>
> The condition models.Q(channel_type="42") is valid in Python, but 
> PostgreSQL requires conditions to work with literal values that match the 
> column type. If channel_type is a CharField, "42" is fine. However, 
> ensure that the value and type match your database schema.
>
>    - Example: If channel_type is actually an integer-like value stored in 
>    a CharField, ensure "42" matches the expected format. 
>
> ------------------------------
> 2. *Potential Issue with Makemigrations* 
>
> Even if the code is correct, makemigrations might not detect the change 
> in the Meta class because migrations only track model-level changes. If 
> makemigrations doesn’t pick up the index:
>
>    - 
>    
>    *Manually Generate the Migration:* Use makemigrations with the --empty 
>    flag and define the index manually.
>    
>    python manage.py makemigrations --empty your_app_name
>    
>    Then, in the generated migration file, define the index:
>    
>    from django.db import migrations, models
>    import django.db.models.expressions
>    
>    class Migration(migrations.Migration):
>        dependencies = [
>            ('your_app_name', 'previous_migration'),
>        ]
>    
>        operations = [
>            migrations.AddIndex(
>                model_name='episode',
>                index=models.Index(
>                    name='ch_last_seen_date',
>                    fields=['last_seen_date'],
>                    condition=models.Q(channel_type='42'),
>                ),
>            ),
>        ]
>    
>    
> ------------------------------
> 3. *Check Your Environment* 
>
> Ensure your Django project environment is up-to-date and consistent:
>
>    - Confirm your django.db.backends.postgresql version supports the 
>    feature. 
>    - Run python manage.py showmigrations to ensure your migrations are 
>    applied. 
>
> ------------------------------
> Debugging Steps 
>    
>    1. 
>    
>    Double-check the condition syntax:
>    
>    condition=models.Q(channel_type='42')
>    
>    Ensure channel_type and '42' match your schema's type expectations.
>    2. 
>    
>    Confirm makemigrations is running in the correct app and picking up 
>    changes:
>    
>    python manage.py makemigrations your_app_name
>    
>    3. 
>    
>    Review your migration file for errors or omissions.
>    
> If the issue persists after these steps, consider upgrading to the latest 
> Django version for potential bug fixes in migration handling. 
>
> On Thu, Dec 5, 2024 at 4:11 PM mccc <[email protected]> wrote:
>
>> Hello,
>>
>> We want to have a partial index (db backend is Postgres) on a 
>> datetimefield, so we added this code to the Meta class:
>>
>> class Episode(models.Model):
>>     channel_type = models.CharField(max_length=16, choices=CHANNEL_TYPES)
>>     last_seen_date = models.DateTimeField(auto_now_add=True)
>>     class Meta:
>>         indexes = [
>>             models.Index(
>>                 name="ch_last_seen_date",
>>                 fields=["last_seen_date"],
>>                 condition=models.Q(channel_type="42"),
>>             ),
>>         ]
>>
>> The problem is that makemigrations doesn't seem to recognize it.
>> What could be the problem? We're on Django 3.2, which I know is old but 
>> as per the documentation this should be fine..
>>
>> Thanks
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion visit 
>> https://groups.google.com/d/msgid/django-users/ee6aa542-aac2-4c78-821b-54d597c80b7cn%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/ee6aa542-aac2-4c78-821b-54d597c80b7cn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> -- 
> Thanks and Regards
>
> J. Ranga Bharath
> cell: 9110334114
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-users/b8323896-aa90-46f4-af45-3b0eaa6ded79n%40googlegroups.com.

Reply via email to