Opened 5 years ago

Closed 5 years ago

#24092 closed Bug (fixed)

New ArrayField from django.contrib.postgres.fields does not correctly save an array of GenericIPAddressField

Reported by: Steve Owned by: Marc Tamlyn
Component: contrib.postgres Version: master
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

ArrayField throws an error when using an array of GenericIPAddressField as the base_field:

from django.contrib.postgres.fields import ArrayField

class MyModel(models.Model):
    ips = ArrayField(base_field=models.GenericIPAddressField(), db_index=True)
>>> test = MyModel()
>>> test.ips = ['10.2.3.4', '1.2.3.4']
>>> test.save()

... truncated...

django.db.utils.ProgrammingError: column "ips" is of type inet[] but expression is of type text[]
LINE 1: ...story" SET "model_id" = 1, "ips" = ARRAY['10...

There also appears to be an issue, when reading from the model (where inet[] data was set in the db itself), instead of a string array of IPs being returned we get this:

>>> test.ips
'{10.2.3.4, 1.2.3.4}'

Change History (5)

comment:1 Changed 5 years ago by Josh Smeaton

Would a cast(x as inet) for each element resolve this? Also, UUID is similar to inet (text value with specific type) so it may also be affected.

comment:2 Changed 5 years ago by Marc Tamlyn

Has patch: set
Owner: set to Marc Tamlyn
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:3 Changed 5 years ago by Marc Tamlyn

Severity: NormalRelease blocker

comment:4 Changed 5 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:5 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 39d95fb6ada99c59d47fa0eae6d3128abafe2d58:

Fixed #24092 -- Widened base field support for ArrayField.

Several issues resolved here, following from a report that a base_field
of GenericIpAddressField was failing.

We were using get_prep_value instead of get_db_prep_value in ArrayField
which was bypassing any extra modifications to the value being made in
the base field's get_db_prep_value. Changing this broke datetime
support, so the postgres backend has gained the relevant operation
methods to send dates/times/datetimes directly to the db backend instead
of casting them to strings. Similarly, a new database feature has been
added allowing the uuid to be passed directly to the backend, as we do
with timedeltas.

On the other side, psycopg2 expects an Inet() instance for IP address
fields, so we add a value_to_db_ipaddress method to wrap the strings on
postgres. We also have to manually add a database adapter to psycopg2,
as we do not wish to use the built in adapter which would turn
everything into Inet() instances.

Thanks to smclenithan for the report.

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