Opened 4 years ago

Closed 4 years ago

#32132 closed Bug (fixed)

ManyToManyField does not respect the PositiveBigIntegerField in m2m intermediate table.

Reported by: Kfir Breger Owned by: David Wobrock
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: models, orm
Cc: David Wobrock 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

When creating a relation between 2 models using PositiveBigIntegerField on Postgresql, the relation table is created using regular ints as the column type. This in turn leads to out of bound error when using large numbers for ids.

from django.contrib.gis.db import models

class Node(models.Model):
    id = models.PositiveBigIntegerField(primary_key=True)
    point = models.PointField()

class Relation(models.Model):
   id = models.PositiveBigIntegerField(primary_key=True)
   nodes = models.ManyToManyField(Node)

The generated table will look like this:

   Column    |  Type   | Collation | Nullable |                    Default                     | Storage | Stats target | Description 
-------------+---------+-----------+----------+------------------------------------------------+---------+--------------+-------------
 id          | integer |           | not null | nextval('osm_relation_nodes_id_seq'::regclass) | plain   |              | 
 relation_id | integer |           | not null |                                                | plain   |              | 
 node_id     | integer |           | not null |                                                | plain   |              | 

As you can see, the PositiveBigInteger is not respected and a regular int is set

Change History (10)

comment:1 by Mariusz Felisiak, 4 years ago

Summary: ManyToMany field does not respect the PositiveBigIntegerField as IDManyToManyField does not respect the PositiveBigIntegerField in m2m intermediate table.
Triage Stage: UnreviewedAccepted

Thanks for the report.

Note that everything works for BigIntegerField().

comment:2 by Kfir Breger, 4 years ago

Thank you for the update. I'll switch to that for now.

comment:3 by David Wobrock, 4 years ago

Cc: David Wobrock added
Has patch: set
Owner: changed from nobody to David Wobrock
Status: newassigned

Hi!

I tried to tackle this issue, since it doesn't seem to complicated as a first glance :)
PR: https://github.com/django/django/pull/13592

comment:4 by Mariusz Felisiak, 4 years ago

Needs tests: set

comment:5 by David Wobrock, 4 years ago

Needs tests: unset

Add unit tests and improved the patch. Ready for another round of reviews :)

comment:6 by Simon Charette, 4 years ago

Patch needs improvement: set

Left some comments regarding the use of related_fields_match_type in tests.

comment:7 by David Wobrock, 4 years ago

Patch needs improvement: unset

Thanks for the comment Simon! I adapted the tests, please see https://github.com/django/django/pull/13592#issuecomment-716212270 for more details

comment:8 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 4ebd633:

Refs #32132 -- Added rel_db_type() tests for auto and integer fields.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In cfc7cd65:

Fixed #32132 -- Fixed column types in m2m intermediary tables for Positive(Big/Small)IntegerFields.

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