Code

Opened 5 years ago

Last modified 8 months ago

#11866 new Bug

non-null FK to a CharField primary key fails when key value is the empty string, giving "[model.fk_col] may not be NULL"

Reported by: andrewbadr Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Model A has a CharField primary_key. Model B has a ForeignKey field to model A. If an instance of A has a row with an empty-string in the primary key column, no B instance can point to it. Attempting to create such a B instance fails with "[model.fk_col] may not be NULL". Repro'd with PG and sqlite3.

Attachments (0)

Change History (8)

comment:1 Changed 5 years ago by andrewbadr

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Seems to be caused by ForeignKey.get_db_prep_save (called from Model.save_base):

    def get_db_prep_save(self, value):
        if value == '' or value == None:
            return None
        ...

Unsure where to go since I don't know the reasoning behind this code. Is this due to forms assigning the empty-string to fields left blank?

comment:2 Changed 4 years ago by andrewbadr

  • Owner changed from andrewbadr to nobody

comment:3 Changed 4 years ago by jacob

  • Triage Stage changed from Unreviewed to Design decision needed

comment:4 in reply to: ↑ description Changed 4 years ago by syko

Replying to andrewbadr:

Model A has a CharField primary_key. Model B has a ForeignKey field to model A. If an instance of A has a row with an empty-string in the primary key column, no B instance can point to it. Attempting to create such a B instance fails with "[model.fk_col] may not be NULL". Repro'd with PG and sqlite3.

I encountered a similar problem in django 1.2.1 using MySQL, but I don't think it has anything to do with foreign keys. The documentation says "Note that empty string values will always get stored as empty strings, not as NULL", which is clearly not the case. Assigning a empty ("") string value to a CharField(null=False, blank=True) and saving the model will throw a "column cannot be NULL" error.
Thus the only way to store empty values in CharFields is having null=True.

comment:5 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:7 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:8 Changed 13 months ago by akaariai

  • Triage Stage changed from Design decision needed to Accepted

This should be fixed at some point. The fix should involve making sure '' works as expected in other parts of Django, too.

Last edited 8 months ago by akaariai (previous) (diff)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.