mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #19299 -- Fixed Nullification of Foreign Keys To CharFields
Thanks tunixman for the report and Baptiste Mispelon and Shai Berger for reviews.
This commit is contained in:
parent
4d4e0ea461
commit
8bbdcc76e4
5 changed files with 32 additions and 7 deletions
|
@ -72,12 +72,21 @@ class BooleanModel(models.Model):
|
|||
string = models.CharField(max_length=10, default='abc')
|
||||
|
||||
|
||||
class PrimaryKeyCharModel(models.Model):
|
||||
string = models.CharField(max_length=10, primary_key=True)
|
||||
|
||||
|
||||
class FksToBooleans(models.Model):
|
||||
"""Model wih FKs to models with {Null,}BooleanField's, #15040"""
|
||||
bf = models.ForeignKey(BooleanModel)
|
||||
nbf = models.ForeignKey(NullBooleanModel)
|
||||
|
||||
|
||||
class FkToChar(models.Model):
|
||||
"""Model with FK to a model with a CharField primary key, #19299"""
|
||||
out = models.ForeignKey(PrimaryKeyCharModel)
|
||||
|
||||
|
||||
class RenamedField(models.Model):
|
||||
modelname = models.IntegerField(name="fieldname", choices=((1, 'One'),))
|
||||
|
||||
|
|
|
@ -22,8 +22,8 @@ from django.utils.functional import lazy
|
|||
|
||||
from .models import (
|
||||
Foo, Bar, Whiz, BigD, BigS, BigInt, Post, NullBooleanModel,
|
||||
BooleanModel, DataModel, Document, RenamedField,
|
||||
VerboseNameField, FksToBooleans)
|
||||
BooleanModel, PrimaryKeyCharModel, DataModel, Document, RenamedField,
|
||||
VerboseNameField, FksToBooleans, FkToChar)
|
||||
|
||||
|
||||
class BasicFieldTests(test.TestCase):
|
||||
|
@ -146,6 +146,17 @@ class ForeignKeyTests(test.TestCase):
|
|||
b = Bar.objects.create(b="bcd")
|
||||
self.assertEqual(b.a, a)
|
||||
|
||||
@test.skipIfDBFeature('interprets_empty_strings_as_nulls')
|
||||
def test_empty_string_fk(self):
|
||||
"""
|
||||
Test that foreign key values to empty strings don't get converted
|
||||
to None (#19299)
|
||||
"""
|
||||
char_model_empty = PrimaryKeyCharModel.objects.create(string='')
|
||||
fk_model_empty = FkToChar.objects.create(out=char_model_empty)
|
||||
fk_model_empty = FkToChar.objects.select_related('out').get(id=fk_model_empty.pk)
|
||||
self.assertEqual(fk_model_empty.out, char_model_empty)
|
||||
|
||||
|
||||
class DateTimeFieldTests(unittest.TestCase):
|
||||
def test_datetimefield_to_python_usecs(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue