Skip to content

Don't automatically collapse `''` and `None` for keys. #1197

Closed
wants to merge 2 commits into from

1 participant

@ghost
ghost commented May 22, 2013

Ticket #19299 which I forgot to send this for.

Many databases treat these differently, including MySQL, and there are
legitimate cases where '' is a valid value in key fields. This change
updates both the ForeignKey class and the row cache to check the
connection feature interprets_empty_strings_as_nulls and will collapse
these two values if that's True, otherwise it will treat them
differently.

Evan Cofsky added some commits Nov 15, 2012
Evan Cofsky Don't automatically collapse `''` and `None` for keys.
Many databases treat these differently, including MySQL, and there are
legitimate cases where `''` is a valid value in key fields. This change
updates both the ForeignKey class and the row cache to check the
connection feature `interprets_empty_strings_as_nulls` and will collapse
these two values if that's `True`, otherwise it will treat them
differently.
386ef64
Evan Cofsky Merged with current Django. 832e1d8
@timgraham timgraham commented on the diff Oct 4, 2013
django/db/models/fields/related.py
@@ -1194,7 +1194,9 @@ def get_default(self):
return field_default
def get_db_prep_save(self, value, connection):
- if value == '' or value == None:
+ if (value == None or
@timgraham
Django member
timgraham added a note Oct 4, 2013

I'd drop the outer parens and do:

if value is None or (value == '' and connection.features.interprets_empty_strings_as_nulls):

This modification also needs a test as the test you've added passes without it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on the diff Oct 4, 2013
django/db/models/query.py
# object must be non-existent - set the relation to None.
- if fields[pk_idx] == None or fields[pk_idx] == '':
+ if (fields[pk_idx] == None or
@timgraham
Django member
timgraham added a note Oct 4, 2013

same style as above -- you'll need to rebase and resolve some conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on the diff Oct 4, 2013
tests/model_forms/models.py
@@ -241,6 +241,14 @@ class FlexibleDatePost(models.Model):
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
posted = models.DateField(blank=True, null=True)
+
+class EmptyKeySource(models.Model):
@timgraham
Django member
timgraham added a note Oct 4, 2013

model_forms isn't the correct test package to put this in. maybe 'queries'? Try searching for "to_field" in the the tests to get an idea of where else that's tested. If it's possible to reuse an existing model, that would be ideal. Otherwise, try to slim down the model fields to the minimal amount needed for testing -- for example, I don't think "db_index=True" is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on the diff Oct 4, 2013
tests/model_forms/tests.py
@@ -1666,6 +1666,15 @@ def test_model_field_that_returns_none_to_exclude_itself_with_explicit_fields(se
self.assertHTMLEqual(six.text_type(CustomFieldForExclusionForm()),
'''<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="10" /></td></tr>''')
+ def test_foreignkeys_which_use_to_field_and_blank_string_key(self):
+ source_n = EmptyKeySource.objects.create(name="")
+ source_a = EmptyKeySource.objects.create(name="a")
@timgraham
Django member
timgraham added a note Oct 4, 2013

souce_a / source_b don't appear necessary for the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ghost
ghost commented Oct 4, 2013

I'll resurrect this and take a look at it over the weekend. I know there was a lot of merging since the change originally was for an old release, so I'll find out why the tests pass now even without the changes.

Thanks :)

@timgraham
Django member

Duplicate of #1690

@timgraham timgraham closed this Feb 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.