Skip to content

Remove the null=True on CharField fields of the PackageURLMixin #36 - #40

Merged
pombredanne merged 4 commits into
masterfrom
36-null-vs-blank
Aug 5, 2020
Merged

Remove the null=True on CharField fields of the PackageURLMixin #36#40
pombredanne merged 4 commits into
masterfrom
36-null-vs-blank

Conversation

@tdruez

@tdruez tdruez commented Jul 28, 2020

Copy link
Copy Markdown
Collaborator

From https://docs.djangoproject.com/en/3.0/ref/models/fields/#null

Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.

This is an issue when adding database constraints based on those fields as '' and null values do not behave the same.

Add a empty=None param on the PackageURL.to_dict method to allow custom value like empty string '' in place of None

Signed-off-by: Thomas Druez tdruez@nexb.com

Add a `empty=None` param on the `PackageURL.to_dict` method to allow custom value like empty string '' in place of None

Signed-off-by: Thomas Druez <tdruez@nexb.com>

@pombredanne pombredanne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tdruez Thank you ++... it makes a lot of sense. I have only one minor nitpicking for your consideration.
@sbs2001 ping... does this make sense for you too since this is used in VulnerableCode ?

raise ValidationError(message)

setattr(self, field_name, value or None)
setattr(self, field_name, value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered setattr(self, field_name, value or '')?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I did. It was my initial approach, but the empty value is now controlled in the above to_dict which makes it unneeded in the setattr.

Comment thread src/packageurl/__init__.py Outdated
data['qualifiers'] = normalize_qualifiers(self.qualifiers,
encode=encode)

if empty is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why using only there?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rephrase your question?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the poor wording ... I mean why would you only translate the empty values to empty if empty is not None?
Why not doing it all the time and therefore removing the test if empty is not None:?

Comment thread src/packageurl/__init__.py Outdated
encode=encode)

if empty is not None:
data = OrderedDict((k, v or empty) for k, v in data.items())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since data is already an OrderedDict... you may want to modify it in place but this is minor:

for k, v in data.items():
    data[k] = v or empty

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in f2e962f

Signed-off-by: Thomas Druez <tdruez@nexb.com>
@tdruez
tdruez requested a review from pombredanne July 29, 2020 05:29
@sbs2001

sbs2001 commented Jul 29, 2020

Copy link
Copy Markdown

@pombredanne This makes total sense and is definitely needed. It will prevent aboutcode-org/vulnerablecode#206 (comment) from happening.

Thanks @tdruez

@tdruez

tdruez commented Jul 29, 2020

Copy link
Copy Markdown
Collaborator Author

@sbs2001 Thanks for your input 👍

tdruez added 2 commits July 29, 2020 10:19
Signed-off-by: Thomas Druez <tdruez@nexb.com>
…#36

Signed-off-by: Thomas Druez <tdruez@nexb.com>
@pombredanne
pombredanne merged commit bcbe63b into master Aug 5, 2020
@tdruez
tdruez deleted the 36-null-vs-blank branch August 5, 2020 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants