bckohan / django-enum

Full and natural support for enumerations as Django model fields.
https://django-enum.rtfd.io
MIT License
40 stars 1 forks source link

Constraints fail when using a name argument #77

Closed twanwv closed 2 weeks ago

twanwv commented 2 weeks ago

When creating an enum property as following

class MyModel(models.Model):

    class TextEnum(models.TextChoices):

        VALUE0 = 'V0', 'Value 0'
        VALUE1 = 'V1', 'Value 1'
        VALUE2 = 'V2', 'Value 2'

    txt_enum = EnumField(TextEnum, name='enum_field', null=True, blank=True, default=None)

It will generate a migration containing the following

operations = [
    migrations.CreateModel(
        name='MyModel',
        fields=[
            ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('enum_field', django_enum.fields.EnumCharField(blank=True, choices=[('V0', 'Value 0'), ('V1', 'Value 1'), ('V2', 'Value 2')], default=None, max_length=2, null=True)),
        ],
        options={
            'constraints': [models.CheckConstraint(condition=models.Q(('txt_enum__in', ['V0', 'V1', 'V2']), ('txt_enum__isnull', True), _connector='OR'), name='entities_MyModel_txt_enum_TextEnum')],
        },
    ),
]

Where the contraint is checking on the txt_enum field and not on the named enum_field field, resulting in migrations that will fail to execute

bckohan commented 2 weeks ago

Thanks for reporting this!

bckohan commented 2 weeks ago

This has been fixed in the 2.0.2 release.

twanwv commented 2 weeks ago

Thanks for the quick fix!