﻿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
21508	ForeignKey cast to int despite being varchar	fiomtec@…	nobody	"Hi!

I have a couple of models connected to a read-only sqlserver DB, like this (trimmed to the important details):


{{{
class Cliente(models.Model):
    codigo = models.CharField(max_length=8, primary_key=True)
    nif = models.CharField(max_length=15)
    nombre = models.CharField(max_length=100, db_column='nombre_apellidos')


class Confirmacion(models.Model):
    cliente = models.ForeignKey(Cliente, db_column='entidad_propietaria', max_length=8)
    [...]other fields[...]
}}}


When I try to find some Confirmacion objects filtering by nif like this:


{{{
Confirmacion.objects.filter(cliente__nif=nif).all()
}}}


it will correctly find some results, but blow up when I try to access them. e.g., this is a dict from one Confirmacion result:

{'cliente_id': 7502, '_state': <django.db.models.base.ModelState object at 0x000000000433C940>, [...] other fields [...]}

the problem is that the correct cliente_id is not 7502, but '007502', and when I try to access that confirmacion.cliente, I get a ""Cliente matching query does not exist"" exception.

I also tried using to_field=""codigo"", same result.

Am I doing something wrong?. Shouldn't the id be a string since the pk is defined as a charfield?.

Thanks."	Bug	closed	Database layer (models, ORM)	1.5	Normal	needsinfo			Accepted	0	0	0	0	0	0
