NOT NULL constraint failed for non-nullable Charfields in xlsx
See original GitHub issueWhen using the .xslx
import, non-nullable CharFields will fail with an exception when their value is left empty. Presumably because the cell has a value of None
in that case.
As seen on StackOverflow where a workaround was provided. IMHO this should simply be fixed instead.
To Reproduce Steps to reproduce the behavior:
- Start the example app
- Edit a book, make the field “author_email” empty.
- Export all books as .xlsx
- Import this .xlsx
sqlite3.IntegrityError: NOT NULL constraint failed: core_book.author_email
Versions (please complete the following information):
- Django Import Export: [2.8.0]
Expected behavior I expect no error. In particular: I expect that the output of an export is a valid input for an import.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
NOT NULL constraint failed error in django despite having null ...
This is the proper way to make it nullable. When you specify default=None , Django ORM does not know if this field is...
Read more >NOT NULL constraint failed vol2 - Django Forum
Hello, I am trying to create a project where all users have opportunity to upload the data to the web app and see...
Read more >SQLite NOT NULL Constraint
This tutorial shows you how to use the SQLite NOT NULL constraint to ensure the values in a column are not NULL.
Read more >Issues with NOT NULL constraint : r/django - Reddit
I'm building a simple app called 'lists' that allows users to create a contact list. I have a model for each Contact, wherein...
Read more >PostgreSQL Not-Null Constraint
We will introduce you to the NULL concept and how to use PostgreSQL not-null constraint to make sure that the values in columns...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, I think we essentially need to inspect both Null and Blank setting on the django field and then set those values appropriately. Then when saving, we need to interpret that correctly. And we can allow users to explicitly override that at the field level (The way they can say saves_null_values, they could also have saves_blank_values)
My thinking is:
-if blank and null are true, empty cells are “” -if blank is true and null is false, empty cells are “” -if blank is false and null is false, empty cells are None, and will fail -if blank is false and null is true, empty cells are None
I think the things we need to do are:
The problem here is that this reduces a little bit of functionality, which is that dialects that have a clear distinction between “” and null (json, yaml) won’t be able to have both blanks and nulls.
I can’t think of any way to get around that except to move things even further up the chain to the importer, but the importer is not aware of the django field. so problematic there.
It might make some sense long term to think about the widget getting information about the blank/null status tbh. I’ll have to think about this some more.
Thanks for working through this issue.
I tested and in this case the
author_email
is exported as empty string “”. In fact, ‘None’ values are always forced to strings on export - this has been in the library from the start.So in the case of Excel, we are exporting as empty string. In Excel, I couldn’t find a way to explicitly set a cell as being an empty string. I tried setting the cell as type ‘Text’ and importing, but it is always interpreted as a ‘None’ (null) value by tablib when importing. So we have the situation where we always export as empty string, but always get back a None value.
To fix this (rather than having to rely on the users using the workaround) I think we would have to include the logic you describe here. Namely that the import process has to be more explicit when dealing with non-nullable fields.