#34727: Error in CharField with TextChoices and missing max_length parameter
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  osamahasanone                      |
                   Type:  Bug        |         Status:  new
              Component:             |        Version:  4.2
  Uncategorized                      |       Keywords:  test, sqlite,
               Severity:  Normal     |  CharField, max_length,choices
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Dear Django Support Team,

 I hope this message finds you well. I am writing to report an issue that I
 encountered while using Django, specifically with the CharField and
 TextChoices combination when specifying choices but not providing the
 max_length parameter.

 **Issue Description:**

 When attempting to add a field of type CharField with TextChoices defined
 but no max_length parameter in the model, running ./manage.py test USING
 SQLITE results in the following error:

 {{{
 django.db.utils.OperationalError: near "None": syntax error
 }}}


 **Reproduction Steps:**


 1. Define a Django model with a CharField, TextChoices, and choices, but
 omit the max_length parameter:


 {{{
 from django.db import models
 from django.utils.text import gettext_lazy as _

 class DummyModel(models.Model):
     class DummyFieldChoices(models.TextChoices):
         OPTION1 = 'option1', _('Option 1')
         OPTION2 = 'option2', _('Option 2')
         OPTION3 = 'option3', _('Option 3')

     dummy_field = models.CharField(
         choices=DummyFieldChoices.choices,
         default=DummyFieldChoices.OPTION1,
     )

 }}}


 2. Run the Django test suite using WITH SQLITE (no issue with Postgres):


 {{{
  ./manage.py test
 }}}


 **Expected Result:**
 The Django test suite should run without any errors, and the model should
 be properly created in the database.


 Actual Result:
 The Django test suite encounters an OperationalError, with the error
 message being:


 {{{
 django.db.utils.OperationalError: near "None": syntax error
 }}}


 **Example of Correct Usage:**


 {{{
 from django.db import models
 from django.utils.text import gettext_lazy as _

 class DummyModel(models.Model):
     class DummyFieldChoices(models.TextChoices):
         OPTION1 = 'option1', _('Option 1')
         OPTION2 = 'option2', _('Option 2')
         OPTION3 = 'option3', _('Option 3')

     dummy_field = models.CharField(
         max_length=10,  # Specify the appropriate maximum length for your
 choices
         choices=DummyFieldChoices.choices,
         default=DummyFieldChoices.OPTION1,
     )


 }}}


 **Additional Information:**

 Django Version: [4.2.3]
 Python Version: [3.11.4]
 Database: [Sqlite]


 I hope this information helps you understand the issue I encountered. If
 you require any further details or assistance, please do not hesitate to
 reach out.


 Thank you for your attention to this matter, and I look forward to your
 response.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34727>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018973c2e47d-ba3a76ab-86d8-468f-bc61-0e0c8315b86b-000000%40eu-central-1.amazonses.com.

Reply via email to