Skip to content
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

Ask for value when changing nullable to not-nullable #2934

Closed
lluishi93 opened this issue Sep 30, 2020 · 4 comments · Fixed by #5400
Closed

Ask for value when changing nullable to not-nullable #2934

lluishi93 opened this issue Sep 30, 2020 · 4 comments · Fixed by #5400

Comments

@lluishi93
Copy link

  1. Create some collection
  2. Create and save some field (string is ok)
  3. Edit that same field, try to make it required
  4. Verify that the following error occurs:
{"errors":[{"message":"alter table `collection_a` modify `foo` varchar(255) not null - ER_INVALID_USE_OF_NULL: Invalid use of NULL value","extensions":{"code":"INTERNAL_SERVER_ERROR"}}]}

Modal remains open and if you close it, required is checked. But if you refresh the page, then required appears unchecked (as if the change has been applied at app-memory level, but not in the database)

Expected behaviour: the user should be able to make a previously created unrequired field required by updating it

EDIT: It seems this happens when you already have created items. I tried to delete the previously created items, then the app allowed me to make that field required. IMHO it should allow to change it anyway.

@lluishi93 lluishi93 changed the title Unable to make a field required after being created Unable to make a field required after being created, only happens when there are existing items for that collection Sep 30, 2020
@lluishi93 lluishi93 changed the title Unable to make a field required after being created, only happens when there are existing items for that collection BETA 3: Unable to make a field required after being created, only happens when there are existing items for that collection Sep 30, 2020
@rijkvanzanten
Copy link
Member

Expected behaviour: the user should be able to make a previously created unrequired field required by updating it

We should figure out what we expect to happen here. You can't require a column to have a value if you already have rows without a value. Otherwise, your value requirement doesn't pass 🤔

@lluishi93
Copy link
Author

@rijkvanzanten based on other CMSs like Contentful, what happens in this situation is that, once you open the items after the field has been changed to requiere, the form automatically detects the missing field and doesn't let the user save it until the new field is filled. If that is something that Directus should allow or not, depends mostly on you, but I think a lot of user would appreciate this behaviour.

@benhaynes
Copy link
Sponsor Member

That makes sense @lluishi93, but Directus mirrors the actual database, and therefore uses allow_null for this. The problem being that the database itself enforces this... and won't allow NULLs in a column with allow_null set to false.

Perhaps we can warn the admin when trying to set this that we'll have to batch update all existing records with the field default value (which may be an empty string). Would that work @rijkvanzanten ?

@rijkvanzanten rijkvanzanten changed the title BETA 3: Unable to make a field required after being created, only happens when there are existing items for that collection Ask for value when changing nullable to not-nullable Oct 30, 2020
@rijkvanzanten rijkvanzanten transferred this issue from another repository Nov 19, 2020
@benhaynes benhaynes added this to the 9.0.0-triage milestone Feb 10, 2021
@rijkvanzanten rijkvanzanten modified the milestones: 9.0.0-triage, v9.0.0 Mar 12, 2021
@benhaynes benhaynes modified the milestones: v9.0.0, v9.0.0-rc.61 Apr 22, 2021
@rijkvanzanten
Copy link
Member

Note to self:

To resolve:

  • Add a new exception for INVALID_USE_OF_NULL + translate per dialect
  • Catch error on alterTable in FieldsService update
  • In app, show custom dialog on INVALID_USE_OF_NULL error with input for null values (default to default_value)
  • Use updateByQuery to set NULL values to provided value + close dialog

rijkvanzanten added a commit that referenced this issue Apr 30, 2021
* Add ContainsNullValues exception abstraction

* Add dialog for null values when disabling non-null

Fixes #2934

* Add translation for CONTAINS_NULL_VALUE error

* Make dialog title translated
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants