Always forbid NULL values in array elements
Currently, when defining an array column in the Contember schema using the .list() method, the .notNull() constraint applies only to the column itself. This means that while the column cannot be NULL, the array may still contain NULL elements. For example:
someValues = c.stringColumn().list().notNull()
allows:
- An empty array:
[] - Arrays with
NULLelements, e.g.,[NULL],[value, NULL]
Proposal
We propose to change the behavior for array columns so that NULL values in the array elements are always forbidden. This change is independent of the .notNull() constraint on the column, which will continue to only ensure that the column itself is non-NULL.
Under this proposal:
-
Column Constraint (
.notNull()):
Prevents the entire column from beingNULL. -
Element Constraint (Always Active for Arrays):
Enforces that noNULLvalues can exist within the array. This could be implemented with a check constraint similar to:CHECK (array_position(foo, null) IS NULL)
Request for Feedback
- Do you agree with the approach of always forbidding
NULLvalues in array elements? - Can you identify any use cases or scenarios where having
NULLelements in an array would be beneficial?
I agree with the proposal that forbids null values in array elements by default. However, there are cases—such as mapping values from sources with nullable fields—where you might want to allow null in the list.
Since columns are nullable by default, it makes sense for lists to allow null values by default too. To provide flexibility, we could add an option to enforce non-null values when needed:
someValues = c.stringColumn().list({ allowNull: false })
This approach offers both consistency and the flexibility to handle nullable data when required.