﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29486	Odd behaviour with ForeignKey to non-integer field since Django 2.0.5 with PostgreSQL	Henrik Ossipoff Hansen	nobody	"I was upgrading one of our projects from 1.11.x to the newest Django 2.0.x when encountering a rather odd thing - all of our tests were literally failing with seemingly a database error:

{{{
django.db.utils.DataError: value ""1111222233334444"" is out of range for type integer
}}}

The field, however, is not an integer field, but rather a char field - so that particular string shouldn't be of any problem - and the fact that it appears to report back being an integer field is rather bizarre.

While I know that this error returned is simply a psycopg2.DataError in disguise, it turns out that this error seem to have been introduced in 2.0.5 specifically - all of our tests passes in 2.0.4.

The slight problem here is that I've been unable to reproduce a minimal working example that can reproduce the error, even though it's really consistent for us.

Our models look something like this (simplified):

{{{
class Giftcard(Model):
    STATUS_CREATED = 'CREATED'
    STATUS_ACTIVATED = 'ACTIVATED'
    STATUS_SUSPENDED = 'SUSPENDED'
    STATUS_EXPIRED = 'EXPIRED'
    STATUS_CLOSED = 'CLOSED'

    STATUS_CHOICE = (
        (STATUS_CREATED, 'Created'),
        (STATUS_ACTIVATED, 'Activated'),
        (STATUS_SUSPENDED, 'Suspended'),
        (STATUS_EXPIRED, 'Expired'),
        (STATUS_CLOSED, 'Closed'),
    )

    code = models.CharField(max_length=16, primary_key=True)
    status = models.CharField(max_length=20, choices=STATUS_CHOICE, default=STATUS_CREATED, db_index=True)

    def status_change(self, reason, to):
        StatusChangeLog.objects.create(giftcard=self, reason=reason, from_status=self.status, to_status=to)

class StatusChangeLog(Model):
    giftcard = models.ForeignKey('giftcards.Giftcard', related_name='status_logs', on_delete=models.PROTECT)
    reason = models.TextField(blank=True, default='')
    from_status = models.CharField(max_length=20, choices=Giftcard.STATUS_CHOICE)
    to_status = models.CharField(max_length=20, choices=Giftcard.STATUS_CHOICE)
}}}

When the Giftcard model is instantiated with a code of ""1111222233334444"" (as a string), saved, and then the status_change method is called, the above exception occurs.

I've been through some of code that went into 2.0.5, but I'm not well enough into the inner workings of Django know exactly what caused this - I just know that this behaviour wasn't in 1.11.x, and also wasn't in 2.0.4."	Bug	closed	Migrations	2.0	Normal	needsinfo			Unreviewed	0	0	0	0	0	0
