-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Potential for errors when deprecating non-nullable fields #17
Comments
Hello @LincolnPuzey, I'll keep this issue open. we need some time to think about it I guess. Is the workaround with @David-Wobrock FYI: Also an intersting case for the linter I guess. I guess right now, the linter fails when using |
@flixx We have only just started trying to do backwards-compatible migrations, so can't say for sure if I think this is mainly a problem when you have an existing project, start doing backwards compatible migrations, and then deprecate a non-nullable field. If you start a project with backwards-compatible migrations enforced from the start, every non-nullable field should have a |
Hi @LincolnPuzey , hi @flixx Indeed an interesting case we didn't give much thought about. My first reflection is that in the case described is it not Django that crashes, but custom code I guess. So the easy way to fixing this would be:
The other solution, as discussed, would be to add a default value on the database level (and basically keep the NOT NULL constraint, since you never want to expect a null). You can still use In all cases, for the linter it's interesting to have a look at cases where Django handles a peculiar case just fine, but the business code rightfully doesn't expect a certain value. |
Say in version
N
of my project I have this model and code that assumesfoo_instance.text_field
is always going to be a string.Then in version
N+1
I then deprecate itThen when version
N
andN+1
are running simultaneously:N+1
inserts a row, it will havetext_field=NULL
N
selects this row, and assumestext_field
is going to be a string, when it is actuallyNone
TypeError
/ValueError
I'm not sure how this could be addressed by the django-deprecate-fields library. Maybe this just needs to be detailed in the docs.
For us the solution is to first use a
AddDefaultValue
operation on the field to add a database-level default. This is so the rows inserted byN+1
get this default value instead ofNULL
, so versionN
can still handle them.The text was updated successfully, but these errors were encountered: