Opened 10 years ago

Closed 10 years ago

#23834 closed Bug (fixed)

ArrayField default is an empty string and not None

Reported by: ddaan Owned by: Marc Tamlyn <marc.tamlyn@…>
Component: contrib.postgres Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by ddaan)

Considering this class (from tests.postgres_tests.models):

class NullableIntegerArrayModel(models.Model):
    field = ArrayField(models.IntegerField(), blank=True, null=True)

When I save it directly after initialization it will result in an error

django.db.utils.DataError: array value must start with "{" or dimension information
LINE 1: ...ests_nullableintegerarraymodel" ("field") VALUES () RETURN...

This test will reproduce the error

    def test_default_null(self):
        instance = NullableIntegerArrayModel()
        instance.save()
        loaded = NullableIntegerArrayModel.objects.get(pk=instance.pk)
        self.assertEqual(loaded.field, None)
        self.assertEqual(instance.field, loaded.field)

Change History (5)

comment:1 by ddaan, 10 years ago

Version: 1.7master

comment:2 by ddaan, 10 years ago

Description: modified (diff)

comment:3 by Marc Tamlyn, 10 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Remco Gerlich, 10 years ago

Only the first two lines of the test are needed, it's the call to .save() that crashes.

The exact same thing happens with a normal IntegerArrayModel, it's not specific to the Nullable one.

get_default() in ArrayField defaults to returning '', it seems that should be None or [] depending on the value of self.null.

Last edited 10 years ago by Remco Gerlich (previous) (diff)

comment:5 by Marc Tamlyn <marc.tamlyn@…>, 10 years ago

Owner: set to Marc Tamlyn <marc.tamlyn@…>
Resolution: fixed
Status: newclosed

In 9a5a4361c577d14cad303019f14ebc6d1ec01674:

Merge pull request #3531 from ddaan/ticket_23834

fixed #23834 -- added test and fix to check for default null on ArrayField

Note: See TracTickets for help on using tickets.
Back to Top